如何在 ASP.NET Core 中获取 HttpContext.Current.Session?

use*_*338 3 asp.net-core

我需要将 MVC 项目迁移到 .net Core,我知道它已从 ASP.net Core 中删除了 System.Web。我需要转换 HttpContext.Current.Session [“name”]!= 在 ASP.NET Core 中为 Null。我添加:使用 Microsoft.AspNetCore.Http 但出现错误。

Alw*_*ner 8

像这样使用:

HttpContext.Session.SetString("priceModel", JsonConvert.SerializeObject(customobject));
var priceDetails = HttpContext.Session.GetString("priceModel");
Run Code Online (Sandbox Code Playgroud)

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

  1. 在ConfigureServices方法中添加Session

    services.AddSession();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 配置方法中的使用会话:

    app.UseSession();
    
    Run Code Online (Sandbox Code Playgroud)