首页 > 解决方案 > 如何在 ASP.NET Core 中获取 HttpContext.Current.Session?

问题描述

我需要将 MVC 项目迁移到 .net Core,我知道它已从 ASP.net Core 中删除了 System.Web。我需要转换 HttpContext.Current.Session ["name"]!= 在 asp.net 核心处为空。我补充说:使用 Microsoft.AspNetCore.Http 但我有一个错误。

标签: asp.net-core

解决方案


像这样使用:

HttpContext.Session.SetString("priceModel", JsonConvert.SerializeObject(customobject));
var priceDetails = HttpContext.Session.GetString("priceModel");

确保启动类中的以下几点:

  1. ConfigureServices 方法中的 AddSession

    services.AddSession();
    
  2. 在配置方法中使用会话:

    app.UseSession();
    

推荐阅读