Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么回发后Cookie总是空的_C#_Asp.net - Fatal编程技术网

C# 为什么回发后Cookie总是空的

C# 为什么回发后Cookie总是空的,c#,asp.net,C#,Asp.net,我试图将一些数据保存到Cookie中,但回发后,如果我在pageload中检查Cookie的值,则该值始终为空 这就是我设置和获取饼干的方式 private static string GetCookie(string name) { return HttpContext.Current.Response != null ? HttpContext.Current.Response.Cookies[name].Value : string.Empty; } private static

我试图将一些数据保存到Cookie中,但回发后,如果我在pageload中检查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);
}
GetCookie()需要使用请求。Cookie不是响应。Cookie

GetCookie()需要使用请求。Cookie不是响应。Cookie

ASP.NET包含两个内在cookie 收藏。已访问该集合 通过Cookies收集 HttpRequest包含cookies 由客户端传输到 Cookie头中的服务器。这个 通过 HttpResponse的Cookies集合 包含在上创建的新cookie 服务器并传输到客户端 在Set Cookie头中

使用添加cookie后 HttpResponse..:.Cookies集合, cookie立即在中提供 HttpRequest..::Cookies 收集,即使响应 尚未发送到客户端

因此,更改您的get以使用请求

ASP.NET包含两个内在cookie 收藏。已访问该集合 通过Cookies收集 HttpRequest包含cookies 由客户端传输到 Cookie头中的服务器。这个 通过 HttpResponse的Cookies集合 包含在上创建的新cookie 服务器并传输到客户端 在Set Cookie头中

使用添加cookie后 HttpResponse..:.Cookies集合, cookie立即在中提供 HttpRequest..::Cookies 收集,即使响应 尚未发送到客户端


因此,更改get以使用请求

从何处调用这些方法?来自ASPX页面中的特定事件?您能在页面或处理程序中显示调用这些方法的代码吗?我认为您需要从请求中获取cookie。您从何处调用这些方法?来自ASPX页面中的特定事件?您能在页面或处理程序中显示调用这些的代码吗?我认为您需要从请求中获取cookie。