在 dotnet core 2.2 中使用 HttpContext

Usage of HttpContext in dotnet core 2.2

我想从会话中获取密钥,但编译器抱怨 class 是静态的,而实际上它不是。谁能帮帮我?

using Microsoft.AspNetCore.Mvc;
using LitOnline_V1.Models;
using Microsoft.AspNetCore.Http;

namespace Test{

    public class GetValidateUer{

        public int GetUserValidation(){

            var isValidated = HttpContext.Session.GetInt32("isValidated");
            return isValidated;
        }
    }
}

严重性代码描述项目文件行抑制状态

Error CS0120 An object reference is required for the non-static field, method, or property 'HttpContext.Session'

希望对您有所帮助

  public class HomeController : Controller
    {
        public IActionResult Index()
        {
            MyMethod(HttpContext);

            // Some Code
        }
    }

    public void MyMethod(Microsoft.AspNetCore.Http.HttpContext context)
    {
        var host = $"{context.Request.Scheme}://{context.Request.Host}";

        // Some Code
    }