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

问题描述 投票:0回答:1

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

asp.net-core
1个回答
0
投票

您是否测试过Microsoft doc,示例如下:>

public const string SessionKeyName = "_Name";
public const string SessionKeyAge = "_Age";
const string SessionKeyTime = "_Time";

 // Requires: using Microsoft.AspNetCore.Http;
    if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
    {
        HttpContext.Session.SetString(SessionKeyName, "The Doctor");
        HttpContext.Session.SetInt32(SessionKeyAge, 773);
    }

    var name = HttpContext.Session.GetString(SessionKeyName);
    var age = HttpContext.Session.GetInt32(SessionKeyAge);
© www.soinside.com 2019 - 2024. All rights reserved.