关于c#:HttpContextBase命名空间找不到

HttpContextBase namespace could not be Found

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   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解决任何帮助? 上面是我的示例代码,正在努力创建购物车。


ASP.NET Core中没有HttpContextBaseHttpContext已经是一个抽象类(请参见此处),已在DefaultHttpContext中实现(请参见GitHub)。 只需使用HttpContext


我不得不像下面这样修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public string GetCartId(HttpContext context)
{
    if (context.Session.GetString(CartSessionKey) == null)
    {
        if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
        {
            context.Session.SetString(CartSessionKey, context.User.Identity.Name);
        }
        else
        {
            var tempCartId = Guid.NewGuid();
            context.Session.SetString(CartSessionKey, tempCartId.ToString());
        }
    }

    return context.Session.GetString(CartSessionKey);
}

它可能会帮助某人:)