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,则会返回主管道的信息