.NET6使用HttpContext.Current

1.启动时添加HttpContextAccessor

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
WebAppContext.Services = builder.Services;

2.添加访问WebAppContext类

class WebAppContext {
    public static IServiceCollection Services;
    public static HttpContext Current {
        get {
            var factory = Services.BuildServiceProvider().GetService(typeof(IHttpContextAccessor));
            return ((HttpContextAccessor)factory).HttpContext;
        }
    }
}
posted @ 2022-04-20 14:19  known  阅读(1240)  评论(1编辑  收藏  举报