0
這個問題似乎已被問了很多次,但我找不到幫助我的答案。ASP.NET Request.Cookies.Get(「Cookie」)返回Null
我有一個母版頁文件的代碼隱藏代碼,當下拉控件更改時,它會設置一個cookie。如果我註釋掉重定向行,我可以看到cookie已正確設置,因爲創建新的cookie併成功輸出其值會顯示已更改下拉菜單的新值。
但是,如果我允許重定向發生,page_load中的代碼將報告cookie集爲空。任何幫助是極大的讚賞!
protected void ThemeSelection_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie themeCookie = new HttpCookie("PreferredTheme");
themeCookie.Expires = DateTime.Now.AddMonths(3);
themeCookie.Value = ThemeSelection.SelectedValue;
Request.Cookies.Add(themeCookie);
HttpCookie cookieCheck = Request.Cookies.Get("PreferredTheme");
Response.Write(cookieCheck.Value);
Response.Redirect(Request.Url.ToString());
}
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme == null)
{
Response.Write("PreferredTheme is null");
}
}
來這裏是爲了解決我的問題,因爲我意識到自己的愚蠢錯誤,看見你了迴應。謝謝! – 2014-09-02 02:27:49
這是非常有益的,thx! – 2017-07-18 15:42:30