c# - 为什么cookie在回发后总是空的

标签 c# asp.net

我正在尝试将一些数据保存到 cookie 中,但是在回发之后,如果我在页面加载中检查 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);
}

最佳答案

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

关于c# - 为什么cookie在回发后总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3470678/

相关文章:

c# - 传递 C# 变量并将其用作 SQL 语句查询中的列值

ASP.NET 自定义网页控件样式 渲染样式规则

c# - tcplistener 没有启动

c# - 弹出浏览器兼容性

ASP.NET TreeView D

javascript - Bootstrap标签输入

ASP.NET 与 VS 2010 : Web Site or Web Application?

c# - xaml 中带有集合的嵌套对象似乎会泄漏到相邻的集合中

C# ref 返回 ref 返回

c# - 从 c# 中的 xml 字符串读取属性的最佳方法是什么