我有一个ASP MVC应用程序,用一些看似简单的代码来保存和检索cookie,但由于某些原因,它们不会持久。控制器中的代码是:
if (System.Web.HttpContext.Current.Response.Cookies["CountryPreference"] == null)
{
HttpCookie cookie = new HttpCookie("CountryPreference");
cookie.Value = country;
cookie.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
并再次加载它:
if (System.Web.HttpContext.Current.Request.Cookies["CountryPreference"] != null)
{
System.Web.HttpContext.Current.Request.Cookies["CountryPreference"].Expires = DateTime.Now.AddYears(1);
data.Country = System.Web.HttpContext.Current.Request.Cookies["CountryPreference"].Value;
}
出于某种原因,cookie总是空的?
发布于 2013-05-13 22:35:38
在简历中,不要使用"Response“来读取cookie,请使用"Request”。
https://stackoverflow.com/questions/456807
复制相似问题