2017-06-27 53 views
3
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命名空間無法找到

+0

您可以使用HttpContextBase abstractContext = new System.Web.HttpContextWrapper(context); – Tomato32

+0

@ Tomato32 ASP.NET Core中不再有'System.Web' – Tseng

+0

你是否在控制器之外執行此操作? – Coemgen

回答

4

ASP.NET Core中沒有HttpContextBaseHttpContext已經是在DefaultHttpContext(見GitHub)中實現的抽象類(參見here)。只需使用HttpContext

+0

'公共字符串GetCartId(HttpContext上下文) '這工作。 –

+0

將它標記爲已接受,如果它解決了您的問題,使其不再出現在「未答覆」部分中 – Tseng

相關問題