【发布时间】:2015-11-20 20:09:25
【问题描述】:
所以 cookie 有效,但在生成 GuID 时 CartID 始终保存为 null...可能我应该在第 3 行添加或更改某些内容。我被堆叠了请帮助。这是代码:
public string GetCartId(HttpContextBase context )
{
if (context.Request.Cookies["CartId"] == null) return "0";
string CartId = context.Request.Cookies["CartId"].Value;
{
if (context.Request.Cookies["CartId"] == null)
{
CartId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie("CartId", CartId);
cookie.Expires = DateTime.Now.AddDays(1);
context.Response.Cookies.Add(cookie);
return CartId.ToString();
}
else
{
return context.Request.Cookies["CartId"].Value;
}
}
【问题讨论】:
-
我需要生成 GuId,以便在 db 表中每个购物车都有一个唯一的 CartId...
-
旁注:请在发布代码以供公众查看时删除幼儿园级别的 cmets - cmets 中绝对没有任何值,例如 `return cartId;// return cart id"