• netcore HttpContext 自定义请求上下文内容


       public static class HttpContext
        {
            private static IHttpContextAccessor _accessor;
    
            public static Microsoft.AspNetCore.Http.HttpContext Current => _accessor.HttpContext;
    
            internal static void Configure(IHttpContextAccessor accessor)
            {
                _accessor = accessor;
            }
        }
    
        public static class StaticHttpContextExtensions
        {
            public static void AddCustomHttpContextAccessor(this IServiceCollection services)
            {
                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            }
    
            public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
            {
                var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
                HttpContext.Configure(httpContextAccessor);
                return app;
            }
        }

    注册服务:

     public void ConfigureServices(IServiceCollection services)
    {
               services.AddHttpContextAccessor();
                services.AddCustomHttpContextAccessor();
    }
    
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
                
              app.UseStaticHttpContext();
     }
  • 相关阅读:
    防火墙实践
    提高工作效率的小技巧
    网络系统参数配置
    linux 文件截取
    python (1) 还不是大全的小问题
    iptables 命令记录
    网络常用端口
    HTTP时间指标
    错误集锦
    access数据库调用
  • 原文地址:https://www.cnblogs.com/wfpanskxin/p/12920466.html
Copyright © 2020-2023  润新知