.Net Core 路由处理
路由基础知识
// 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.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); }
终结点
-
- 用于 Razor Pages 的 MapRazorPages
- 用于控制器的 MapControllers
- 用于 SignalR 的 MapHub
- 用于 gRPC 的 MapGrpcService
app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapHealthChecks("/healthz").RequireAuthorization(); endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); });
终结点元数据
-
- 可以通过路由感知中间件来处理元数据。
- 元数据可以是任意的 .NET 类型。
// 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.UseRouting(); app.Use(next => context => { var endpoint = context.GetEndpoint(); if (endpoint?.Metadata.GetMetadata<AuditPolicyAttribute>()?.NeedsAudit ==true) { Console.WriteLine("开始处理事务逻辑"); Console.WriteLine($"ACCESS TO SENSITIVE DATA AT: {DateTime.UtcNow}"); } return next(context); }); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello world!"); }); // Using metadata to configure the audit policy. endpoints.MapGet("/sensitive", async context => { await context.Response.WriteAsync($"sensitive data{DateTime.UtcNow}"); }) .WithMetadata(new AuditPolicyAttribute(needsAudit: true)); }); } } public class AuditPolicyAttribute : Attribute { public AuditPolicyAttribute(bool needsAudit) { NeedsAudit = needsAudit; } public bool NeedsAudit { get; } }
比较终端中间件和路由
-
-
这两种方法都允许终止处理管道:终端中间件允许在管道中的任意位置放置中间件:
- 中间件通过返回而不是调用 next 来终止管道。
- 终结点始终是终端。
-
终端中间件允许在管道中的任意位置放置中间件:
-
- 终结点在 UseEndpoints 位置执行。
- 终端中间件允许任意代码确定中间件匹配的时间:
-
- 自定义路由匹配代码可能比较复杂,且难以正确编写。
- 路由为典型应用提供了简单的解决方案。
- 大多数应用不需要自定义路由匹配代码。
- 带有中间件的终结点接口,如 UseAuthorization 和 UseCors。
-
- 通过 UseAuthorization 或 UseCors 使用终端中间件需要与授权系统进行手动交互
-
这两种方法都允许终止处理管道:终端中间件允许在管道中的任意位置放置中间件:
设置传统路由
app.UseEndpoints(endpoints => { app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); });
REST Api 的属性路由
// 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.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
- Route[]
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { [Route("Index")] public string Index(int? id) { return "Test"; } }
[ApiController] [Route("[controller]/[action]")] public class WeatherForecastController : ControllerBase { public string Index(int? id) { return "Test"; } }
[ApiController] public class WeatherForecastController : ControllerBase { [Route("[controller]/Index")] public string Index(int? id) { return "Test"; } }
- Http[Verb]
[ApiController] public class WeatherForecastController : ControllerBase { [HttpGet("[controller]/Index")] public string Index(int? id) { return "Test"; } }
- Route[]和Http[Verb]混合使用
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { [HttpGet("Index")] public string Index(int? id) { return "Test"; } }