【发布时间】:2016-06-10 22:51:25
【问题描述】:
我有一些读取和写入 cookie 值的 asp.net 页面。在页面的生命周期中,它可能会更新 cookie 值,然后需要在代码中进一步读取它。我发现在页面刷新之前它不会获得 cookie 的最新值。有没有解决的办法?这是我用来设置和获取值的代码。
public static string GetValue(SessionKey sessionKey)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiePrefix];
if (cookie == null)
return string.Empty;
return cookie[sessionKey.SessionKeyName] ?? string.Empty;
}
public static void SetValue(SessionKey sessionKey, string sessionValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiePrefix];
if (cookie == null)
cookie = new HttpCookie(cookiePrefix);
cookie.Values[sessionKey.SessionKeyName] = sessionValue;
cookie.Expires = DateTime.Now.AddHours(1);
HttpContext.Current.Response.Cookies.Set(cookie);
}
【问题讨论】: