using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace Masuit.Tools.Core.Net { /// /// 实现类似于.NET Framework的HttpContext静态对象的中间件对象 /// public static class StaticHttpContextExtensions { /// /// 注入HttpContext静态对象,方便在任意地方获取HttpContext,services.AddHttpContextAccessor(); /// /// public static void AddStaticHttpContext(this IServiceCollection services) { services.AddSingleton(); } /// /// 注入HttpContext静态对象,方便在任意地方获取HttpContext,app.UseStaticHttpContext(); /// /// /// public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app) { var httpContextAccessor = app.ApplicationServices.GetRequiredService(); HttpContext2.Configure(httpContextAccessor); return app; } } }