我有以下设置 cookie 的代码:
string locale = ((DropDownList)this.LoginUser.FindControl("locale")).SelectedValue;
HttpCookie cookie = new HttpCookie("localization",locale);
cookie.Expires= DateTime.Now.AddYears(1);
Response.Cookies.Set(cookie);
但是,当我尝试读取 cookie 时,值为 Null。 cookie 存在。我永远不会通过以下 if 检查:
if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))
帮忙吗?
最佳答案
检查是在回发后完成的?如果是这样,您应该改为从 Request 集合中读取 cookie。
通过将 cookie 添加到 Response.Cookies 并将它们从 Request.Cookies 中读回,cookies 会持久保存到浏览器中。
添加到 Response 的 cookie 只有在页面处于同一请求时才能读取。
关于c# - Cookie 在 ASP.net 中失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/207878/