ASP.NET Core中间件管道MAP

Map扩展用作约定来创建管道分支。Map基于给定请求路径的匹配项来创建请求管道分支。如果请求路径以给定路径开头,则执行分支。

 public class Startup
    {
        private readonly IConfiguration _iConfiguration;

        public Startup(IConfiguration iConfiguration)
        {
            _iConfiguration = iConfiguration;
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddRazorPages();
            services.AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(_iConfiguration.GetConnectionString("DefaultConnectionString"));
            });
            services.AddScoped<IService<Order>, OrderService>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.Map("/map1", HandleMapTest1);
            app.Map("/map2", HandleMapTest2);
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Order}/{action=Index}/{id?}");

                
                //endpoints.MapGet("/", async context =>
                //{
                //    await context.Response.WriteAsync("Hello World!");
                //});
            });
        }

        private static void HandleMapTest1(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("Map test 1"); });
        }

        private static void HandleMapTest2(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("Map test 2"); });
        }

    }

Startup类中定义了两个Map,当获取输入/map1时
在这里插入图片描述
输入/map2
在这里插入图片描述
输入其他
在这里插入图片描述
使用Map时,将从HttpRequest.Path中删除匹配的路径短,并针对每个请求将路径追加到HttpRequest.PathBase。

Map还支持嵌套

Startup类

  public class Startup
    {
        private readonly IConfiguration _iConfiguration;

        public Startup(IConfiguration iConfiguration)
        {
            _iConfiguration = iConfiguration;
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddRazorPages();
            services.AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(_iConfiguration.GetConnectionString("DefaultConnectionString"));
            });
            services.AddScoped<IService<Order>, OrderService>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.Map("/map1", map1 => { map1.Map("/map3", HandleMapTest1); });
            app.Map("/map2", map2 => { map2.Map("/map4", HandleMapTest2); });
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Order}/{action=Index}/{id?}");

                
                //endpoints.MapGet("/", async context =>
                //{
                //    await context.Response.WriteAsync("Hello World!");
                //});
            });
        }

        private static void HandleMapTest1(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("Map test 1"); });
        }

        private static void HandleMapTest2(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("Map test 2"); });
        }

    }

其中

app.Map("/map1", map1 => { map1.Map("/map3", HandleMapTest1); });
app.Map("/map2", map2 => { map2.Map("/map4", HandleMapTest2); });

只有输入/map1/map3 /map2/map4才能起作用
在这里插入图片描述
当输入/map1时,会报错,找不到 localhost 的网页。

MapWhen

MapWhen基于给定谓词的结果创建请求管道分支。Func<httpContext,bool>类型的任何谓词均可用于将请求映射到管道的新分支。

    public class Startup
    {
        private readonly IConfiguration _iConfiguration;

        public Startup(IConfiguration iConfiguration)
        {
            _iConfiguration = iConfiguration;
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddRazorPages();
            services.AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(_iConfiguration.GetConnectionString("DefaultConnectionString"));
            });
            services.AddScoped<IService<Order>, OrderService>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            //app.Map("/map1", map1 => { map1.Map("/map3", HandleMapTest1); });
            //app.Map("/map2", map2 => { map2.Map("/map4", HandleMapTest2); });
            app.MapWhen(context => context.Request.Query.ContainsKey("hello"), HandlerHello);
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Order}/{action=Index}/{id?}");


                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }

        //private static void HandleMapTest1(IApplicationBuilder app)
        //{
        //    app.Run(async context => { await context.Response.WriteAsync("Map test 1"); });
        //}

        //private static void HandleMapTest2(IApplicationBuilder app)
        //{
        //    app.Run(async context => { await context.Response.WriteAsync("Map test 2"); });
        //}

        private static void HandlerHello(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                var hello = context.Request.Query["hello"];
                await context.Response.WriteAsync($"hello ={hello}");
            });
        }

    }

主要是

 app.MapWhen(context => context.Request.Query.ContainsKey("hello"), HandlerHello);

当输入中存在hello的参数时,就会触发handlerHello方法
在这里插入图片描述

UseWhen

UseWhen也基于给定谓词的结果创建请求管道分支。与MapWhen不同的是,如果这个分支发生短路或者找不到请求,则会重新加入主管道。

app.UseWhen(context => context.Request.Query.ContainsKey("hello"), HandlerHello);

当输入/?hello=world后,仍然是正常工作
在这里插入图片描述
如果输入?/hel=world,则会返回主管道的信息
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值