.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;
}
}
}