1.创建Cookies方法
if (Request.Cookies["Products"] == null) //判断Products(cookies)是否存在
{
HttpCookie
oCookie = new HttpCookie("Products");
oCookie.Expires =
DateTime.Now.AddHours(24); //设置Products(cookies)过去时间
oCookie.Value = pro_id.ToString();
Response.Cookies.Add(oCookie);
}
else
{
bool bExists =
false;
char[] sep = { ',' };
HttpCookie oCookie = (HttpCookie)Request.Cookies["Products"];
//实例cookie
oCookie.Expires =
DateTime.Now.AddHours(24); //设置Products(cookies)过去时间
string sProdID = oCookie.Value.ToString();
//Products(cookies)包含的值
string[] arrCookie =
sProdID.Split(sep);
for (int i = 0; i < arrCookie.Length;
i++)
{
if (arrCookie[i].Trim()
== pro_id.ToString().Trim())
{
bExists = true;
}
}
if
(!bExists)
{
if
(oCookie.Value.Length == 0)
{
oCookie.Value =
pro_id.ToString();
}
else
{
oCookie.Value =
oCookie.Value + "," + pro_id;
}
}
Response.Cookies.Add(oCookie);
//Products(cookies)添加进cookies
}
2.清除cookies方法
HttpCookie cookie = Request.Cookies["cookieNmae"];
cookie.Expires
= DateTime.Today.AddDays(-1);
//重要:
Response.Cookies.Add(cookie);
本文内容来源:互联网博客