本文概述
ASP.NET Cookie是一小段文本,用于存储用户特定的信息。每当用户访问站点时,Web应用程序都可以读取此信息。
当用户请求网页时,网络服务器不仅发送页面,还发送包含日期和时间的cookie。该cookie存储在用户硬盘上的文件夹中。
当用户再次请求该网页时,浏览器会在硬盘驱动器上查找与该网页关联的cookie。浏览器为用户访问的每个不同站点存储单独的cookie。
注意:Cookie限于小尺寸,只能用于存储4 KB(4096字节)的文本。
在ASP.NET应用程序中有两种存储cookie的方法。
- 饼干集合
- HttpCookie
我们可以将Cookie添加到Cookies集合中,也可以通过创建HttpCookie类的实例来添加。除了HttpCookie要求Cookie名称作为构造函数的一部分外,两者的工作原理相同。
HttpCookie示例
在以下示例中,我们将在HttpCookie类的帮助下创建并添加cookie。
// CookieExample.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="CookieExample.aspx.cs" Inherits="CoockieExample.CookieExample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
码
// CookieExample.aspx.cs
using System;
using System.Web;
namespace WebFormsControlls
{
public partial class CookieExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//-------------- Creating Cookie --------------------------//
// Creating HttpCookie instance by specifying name "student"
HttpCookie cokie = new HttpCookie("student");
// Assigning value to the created cookie
cokie.Value = "Rahul Kumar";
// Adding Cookie to the response instance
Response.Cookies.Add(cokie);
//--------------- Fetching Cookie -------------------------//
var co_val = Response.Cookies["student"].Value;
Label1.Text = co_val;
}
}
}
Cookie收集示例
在以下示例中,我们将cookie直接添加到Cookies集合中。
// Default.aspx
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="CoockieExample._Default" %>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Select Brand Preferences"></asp:Label>
<br />
<br />
<asp:CheckBox ID="apple" runat="server" Text="Apple" />
<br />
<asp:CheckBox ID="dell" runat="server" Text="Dell" />
<br />
<asp:CheckBox ID="lenevo" runat="server" Text="Lenevo" />
<br />
<asp:CheckBox ID="acer" runat="server" Text="Acer" />
<br />
<asp:CheckBox ID="sony" runat="server" Text="Sony" />
<br />
<asp:CheckBox ID="wipro" runat="server" Text="Wipro" />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<p>
<asp:Label ID="Label2" runat="server"></asp:Label>
</p>
</form>
代码背后
// Default.aspx.cs
using System;
using System.Web.UI;
namespace CoockieExample
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Setting expiring date and time of the cookies
Response.Cookies["computer"].Expires = DateTime.Now.AddDays(-1);
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = "";
// --------------- Adding Coockies ---------------------//
if (apple.Checked)
Response.Cookies["computer"]["apple"] = "apple";
if (dell.Checked)
Response.Cookies["computer"]["dell"] = "dell";
if (lenevo.Checked)
Response.Cookies["computer"]["lenevo"] = "lenevo";
if (acer.Checked)
Response.Cookies["computer"]["acer"] = "acer";
if (sony.Checked)
Response.Cookies["computer"]["sony"] = "sony";
if (wipro.Checked)
Response.Cookies["computer"]["wipro"] = "wipro";
// --------------- Fetching Cookies -----------------------//
if (Request.Cookies["computer"].Values.ToString() != null)
{
if (Request.Cookies["computer"]["apple"] != null)
Label2.Text += Request.Cookies["computer"]["apple"] + " ";
if (Request.Cookies["computer"]["dell"] != null)
Label2.Text += Request.Cookies["computer"]["dell"] + " ";
if (Request.Cookies["computer"]["lenevo"] != null)
Label2.Text += Request.Cookies["computer"]["lenevo"] + " ";
if (Request.Cookies["computer"]["acer"] != null)
Label2.Text += Request.Cookies["computer"]["acer"] + " ";
if (Request.Cookies["computer"]["sony"] != null)
Label2.Text += Request.Cookies["computer"]["sony"] + " ";
if (Request.Cookies["computer"]["wipro"] != null)
Label2.Text += Request.Cookies["computer"]["wipro"] + " ";
}else Label2.Text = "Please select your choice";
Response.Cookies["computer"].Expires = DateTime.Now.AddDays(-1);
}
}
}
输出:
本示例将选择的值存储为cookie。
评论前必须登录!
注册