Core 父类 Api 访问HttpContext 为NULL

时间:2021-08-13 10:07:01   收藏:0   阅读:32

 

新建Helper类

    public static class HelperHttpContext
    {
        public static IServiceCollection serviceCollection;

        public static HttpContext Current
        {
            get
            {
                object factory = serviceCollection.BuildServiceProvider().GetService(typeof(IHttpContextAccessor));
                HttpContext context = ((IHttpContextAccessor)factory).HttpContext;
                return context;
            }
        }
    }

然后再Startup  ConfigServices中 添加以下代码

// 增加Http组件
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
HelperHttpContext.serviceCollection = services; 


原文:https://www.cnblogs.com/RecordOnLog/p/15135949.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!