.Net Core在类库中使用当前HttpContext

Posted sky-net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.Net Core在类库中使用当前HttpContext相关的知识,希望对你有一定的参考价值。

HttpContext.Current在ASP.NET Core中不再存在,但是IHttpContextAccessor可以在依赖关系中注入一个新的并用于检索当前HttpContext

使用方法如下:

public class MyComponent : IMyComponent
{
    private readonly IHttpContextAccessor _contextAccessor;

    public MyComponent(IHttpContextAccessor contextAccessor)
    {
        _contextAccessor = contextAccessor;
    }

    public string GetDataFromSession()
    {
        return _contextAccessor.HttpContext.Session.GetString(*KEY*);
    }
}

 

以上是关于.Net Core在类库中使用当前HttpContext的主要内容,如果未能解决你的问题,请参考以下文章