protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["ShoppingCartss"] != null)
{
}
}
public void WriteCookies()
{
HttpContext.Current.Response.Cookies["ShoppingCartss"]["ProductID"] = "dd";
HttpContext.Current.Response.Cookies["ShoppingCartss"]["Amount"] = "ff";
HttpContext.Current.Response.Cookies["ShoppingCartss"].Expires = DateTime.Now.AddDays(60); }
public void Clear()
{
//删除Cookies
HttpContext.Current.Response.Cookies["ShoppingCartss"].Expires = DateTime.Parse("1980-01-01");
}
protected void Button1_Click(object sender, EventArgs e)
{
WriteCookies();
}
protected void Button2_Click(object sender, EventArgs e)
{
Clear();
}
为什么page_load时候Request.Cookies["ShoppingCartss"]这个对象就不是为null,现在情况是我调用了Claar()这个方法,但Request.Cookies["ShoppingCartss"]对象为什么不等于Null,请大侠们指点迷津,谢谢哦
{
if (Request.Cookies["ShoppingCartss"] != null)
{
}
}
public void WriteCookies()
{
HttpContext.Current.Response.Cookies["ShoppingCartss"]["ProductID"] = "dd";
HttpContext.Current.Response.Cookies["ShoppingCartss"]["Amount"] = "ff";
HttpContext.Current.Response.Cookies["ShoppingCartss"].Expires = DateTime.Now.AddDays(60); }
public void Clear()
{
//删除Cookies
HttpContext.Current.Response.Cookies["ShoppingCartss"].Expires = DateTime.Parse("1980-01-01");
}
protected void Button1_Click(object sender, EventArgs e)
{
WriteCookies();
}
protected void Button2_Click(object sender, EventArgs e)
{
Clear();
}
为什么page_load时候Request.Cookies["ShoppingCartss"]这个对象就不是为null,现在情况是我调用了Claar()这个方法,但Request.Cookies["ShoppingCartss"]对象为什么不等于Null,请大侠们指点迷津,谢谢哦
你可以设置cookies的有效期。。这个好像是在配置文件里面改的。你查一下cookies的特性和设置方法。
只是设置过期 判断一下过期
HttpContext.Current.Response.Cookies["ShoppingCartss"].Expires = DateTime.Parse("1980-01-01");
你这个是赋值。根本不是删除啊。
/// 清空cookie
/// </summary>
public static void ClertCookie() {
if (HttpContext.Current.Request[CookieName] != null) {
HttpCookie cookie = new HttpCookie(CookieName);
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 0, 0, 0); HttpContext.Current.Response.AppendCookie(cookie);
cookie.Expires = dt.Add(ts);
}
不能直接修改或删除Cookie,只能创建一个新的Cookie