// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env , ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 按照规则名称应用跨域规则,必须在 UseRouting 前应用 app.UseCors("global_cors"); app.UseStaticFiles(); app.UseRouting(); // 管道中应用认证中间件 app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // 设置 MyHttpContext var accessor = app.ApplicationServices.GetRequiredService <IHttpContextAccessor>(); MyHttpContext.Configure(accessor); // 设置日志处理,需要安装 Microsoft.Extensions.Logging.Log4Net.AspNetCore 包 loggerFactory.AddLog4Net(); }
public static IApplicationBuilder UseCustomHttpContextAccessor(this IApplicationBuilder app) { MyHttpContext.Configure(app.ApplicationServices.GetRequiredService <IHttpContextAccessor>()); return(app); }