cookie删除的怪异问题
最近在解决一个多语言的问题。 用到了cookie. 于是就想彻底了解一下cookie.没想到遇到了一个非常奇怪的问题,一直不得其解,就写这篇文章来这询问各位了。环境:
vs2008和IE6
页面代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
UICulture="auto" Culture="auto" meta:resourcekey="PageResource1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="lblUserName" runat="server" meta:resourcekey="lblUserName" Text="Label"></asp:Label>
<%--<asp:Label ID="lblUserName" runat="server" Text="<%$ Resources:Default.aspx.zh-CN.resx,lblUserName%>"></asp:Label>--%>
<asp:Image ImageUrl="~/Images/213hws7.gif" ID="imgNew" runat="server" meta:resourcekey="imgNew" />
<asp:DropDownList ID="ddlMultiLanguage" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMultiLanguage_SelectedIndexChanged">
<asp:ListItem Value="en-us" Text="Englist" Selected="True"></asp:ListItem>
<asp:ListItem Value="zh-cn" Text="简体中文"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
后台代码如下:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["language_cookie"] != null && Request.Cookies["language_cookie"].Value != string.Empty)
{
string language_cookie = Request.Cookies["language_cookie"].Value.ToLower();
this.ddlMultiLanguage.SelectedValue = Request.Cookies["language_cookie"].Value.ToLower();
}
else
{
this.ddlMultiLanguage.SelectedValue = Request.Headers["accept-language"].Split(",".ToCharArray())[0].ToString().ToLower();
}
}
}
protected void ddlMultiLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (Request.Cookies["language_cookie"] != null && Request.Cookies["language_cookie"].Value != string.Empty)
{
Request.Cookies["language_cookie"].Value = this.ddlMultiLanguage.SelectedValue;
Request.Cookies["language_cookie"].Expires = DateTime.Now.AddDays(10);
}
else
{
Response.Cookies["language_cookie"].Value = this.ddlMultiLanguage.SelectedValue;
Response.Cookies["language_cookie"].Expires = DateTime.Now.AddDays(10);
}
String selectedLanguage = this.ddlMultiLanguage.SelectedValue;
UICulture = selectedLanguage;
Culture = selectedLanguage;
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(selectedLanguage);
}
protected void Button1_Click(object sender, EventArgs e)
{
//Request.Cookies.Clear();
//Response.Cookies.Clear();
//Response.Cookies.Remove("language_cookie");
//Response.Cookies["language_cookie"].Expires = DateTime.Now.AddYears(-1000);
Response.Cookies["language_cookie"].Expires = DateTime.Now.AddDays(-100);
}
protected override void InitializeCulture()
{
base.InitializeCulture();
}
}
问题:
我在Button1_Click事件中,设置了cookie的过期时间,这样浏览器就可以删除cookie. 我查看了一下下面两个文件夹
C:\Documents and Settings\user\Cookies
C:\Documents and Settings\user\Local Settings\Temporary Internet Files
里边的cookie确实不存在了。
但是,当我再次启动程序,发现在 Page_Load事件中的Request.Cookies["language_cookie"]竟然还存在的。我个人觉得在点击button触发Button1_Click事件后,cookie应该是不删除了,再次启动程序的时候Request.Cookies["language_cookie"]应该是不存在的。所以很疑惑。请各位指教。
多谢多谢!
补充:当我设置一个很大的值Response.Cookies["language_cookie"].Expires = DateTime.Now.AddYears(-1000); 效果就与推断的一样。Request.Cookies["language_cookie"]就真的不存在了。
--------------------编程问答--------------------
按理说这样已经可以了。
Response.Cookies["language_cookie"].Expires = DateTime.Now.AddDays(-100);
--------------------编程问答-------------------- 看不明白 --------------------编程问答-------------------- ding --------------------编程问答-------------------- protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["language_cookie"] != null)
{
HttpCookie myCookie = new HttpCookie("language_cookie");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
} --------------------编程问答-------------------- 为什么数字要设置为负的
--------------------编程问答-------------------- tulong403看不明白?首先感谢你的回复 请问什么地方不明白,你可以把代码复制下来,在自己本地尝试一下。 --------------------编程问答-------------------- 设置cookie 的管理相关。 在 config里设置吧。 --------------------编程问答-------------------- 能说的具体一些吗?如何在config里设置?
补充:.NET技术 , ASP.NET