新建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;