【发布时间】:2017-06-27 08:20:00
【问题描述】:
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
对在 asp.net 核心中使用 HttpContextBase 的工作有任何帮助吗?以上是我创建购物车的示例代码。
【问题讨论】:
-
可以使用 HttpContextBase abstractContext = new System.Web.HttpContextWrapper(context);
-
@Tomato32 在 ASP.NET Core 中没有
System.Web了 -
您是在控制器之外执行此操作吗?
-
我不知道它是否重要,但不保证 guid 是随机的,它们只保证是唯一的。
标签: c# asp.net-core asp.net-core-mvc