为什么在回发后cookie总是为空

Gig*_*aPr 1 c# asp.net

我试图将一些数据保存到cookie中,但在回发后如果我在pageload中检查cookie的值,则该值始终为null

这就是我设置和获取cookie的方式

private static string GetCookie(string name)
{
    return HttpContext.Current.Response != null ? HttpContext.Current.Response.Cookies[name].Value : string.Empty;
}

private static void SetCookie(string name, string value)
{
    HttpContext.Current.Response.Cookies[name].Value = value;
    HttpContext.Current.Response.Cookies[name].Expires = DateTime.Now.AddDays(ExpireTimeInDays);
}
Run Code Online (Sandbox Code Playgroud)

小智 12

GetCookie()需要使用Request.Cookie而不是Response.Cookie