找不到HttpContextBase名称空间

时间:2017-06-27 08:20:01

标签: c# asp.net-core asp.net-core-mvc

   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的任何帮助吗?上面是我的示例代码我正在努力创建一个购物车。

2 个答案:

答案 0 :(得分:20)

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

答案 1 :(得分:1)

我必须进行如下修改

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);
}

它可能会帮助某人:)