【发布时间】:2012-07-10 11:58:27
【问题描述】:
我正在服务器上添加 cookie:
private void AddCookie(int id)
{
HttpCookie cookie = new HttpCookie("wmpayment");
cookie.Value = id.ToString();
cookie.Expires = DateTime.Now.AddDays(2);
this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
}
但是当我从请求中读取 cookie - cookie.Expire 等于日期 01.01.0001
public static int WMPendingOrder
{
get
{
var cookie = HttpContext.Current.Request.Cookies["wmpayment"];
int id = 0;
DateTime exp;
if (cookie != null)
{
DateTime.TryParse(cookie.Expires.ToString(), out exp);
if (DateTime.Now < exp)
int.TryParse(cookie.Value, out id);
}
return id;
}
}
日志:COOKIE.Name:wmpayment COOKIE.Value:0 COOKIE.Expire:01.01.0001 0:00:00 我不明白这是什么问题。
【问题讨论】:
-
这条线有问题.."比我读到的-expires = 01.01.0001:"..请更改它..很难理解你想要什么..
-
我发现本教程很有帮助,您可以将其视为替代方法:code-inside.de/blog-in/2010/10/19/…
-
我解决了这个问题如下。只是简单地重置 cookie 的值 - 将 orderId 设置为“0”。但非常有趣的是为什么浏览器在服务器上删除 cookie 后不删除它。
标签: asp.net .net asp.net-mvc-3 cookies