在 asp.net core 中使用 HttpContext.Current

首先不推荐这样做,仅限无奈需要这样做的时候,再这样做。。。

参考:https://www.cnblogs.com/maxzhang1985/p/6186455.html

https://www.cnblogs.com/linezero/p/6801602.html

https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core/40029302#40029302

复制代码
public static class HttpContextStatic
{
    public static IServiceProvider ServiceProvider;

    public static void Configure(IServiceProvider serviceProvider)
    {
        ServiceProvider = serviceProvider;
    }

    public static HttpContext Current
    {
        get
        {
            var factory = ServiceProvider.GetService(typeof(IHttpContextAccessor));
            var accessor = (HttpContextAccessor) factory;
            return accessor?.HttpContext;
        }
    }
}
复制代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    HttpContextStatic.Configure(app.ApplicationServices);
}
posted @   不是豆豆  阅读(876)  评论(0编辑  收藏  举报
努力加载评论中...
友情链接:迷途


点击右上角即可分享
微信分享提示