Cookie实验
1在同一页面中添加删除后在当前页好象还在。
2如果这样。可以成功的删除cookie但,删除前要确认下cookie是否在
if(Request.QueryString["a"]==null)
{
Response.Cookies["aa"]["bb"] = "ss";
Response.Cookies["aa"]["cc"] = "kk";
}
HttpCookie c = Request.Cookies["aa"];
if (c!=null)
{
Response.Write("readd");
c.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(c);
}
if(Request.Cookies["aa"]==null)
{
Response.Write("<br>COOKIE不在了!!!");
}
else
{
Response.Write("<br>COOKIE还在="+Request.Cookies["aa"]["cc"]);
}
{
Response.Cookies["aa"]["bb"] = "ss";
Response.Cookies["aa"]["cc"] = "kk";
}
HttpCookie c = Request.Cookies["aa"];
if (c!=null)
{
Response.Write("readd");
c.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(c);
}
if(Request.Cookies["aa"]==null)
{
Response.Write("<br>COOKIE不在了!!!");
}
else
{
Response.Write("<br>COOKIE还在="+Request.Cookies["aa"]["cc"]);
}
这个写法成功删除cookie
if(Request.QueryString["a"]==null)
{
Response.Cookies["aa"]["bb"] = "ss";
Response.Cookies["aa"]["cc"] = "kk";
Response.Cookies["aa"].Expires=DateTime.Now.AddDays(1);
}
Response.Cookies["aa"].Expires=DateTime.Now.AddDays(-1);
if(Request.Cookies["aa"]==null)
{
Response.Write("<br>COOKIE不在了!!!");
}
else
{
Response.Write("<br>COOKIE还在="+Request.Cookies["aa"]["cc"]);
}
{
Response.Cookies["aa"]["bb"] = "ss";
Response.Cookies["aa"]["cc"] = "kk";
Response.Cookies["aa"].Expires=DateTime.Now.AddDays(1);
}
Response.Cookies["aa"].Expires=DateTime.Now.AddDays(-1);
if(Request.Cookies["aa"]==null)
{
Response.Write("<br>COOKIE不在了!!!");
}
else
{
Response.Write("<br>COOKIE还在="+Request.Cookies["aa"]["cc"]);
}
这样呢,简洁多了。Cookie里的但值虽然没了,但判断居然不是null,带QueryString参数访问后。仍然显示cookie还在。
奇怪死我了。
摇头想了想,应该是我用的是cookie集合,而第二种写法上认为是一个单键cookie了。而不是那个集合名,但为何又把cookie集合的值清除了呢。