Core端点路由在ASP.NET中的作用有哪些?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

端点路由(Endpoint Routing)最早出现在ASP.NET Core2.2,在ASP.NET Core3.0提升为一等公民。
Endpoint Routing的动机
在端点路由出现之前,我们一般在请求处理管道的末尾,定义MVC中间件解析路由。这种方式意味着在处理管道中,MVC中间件之前的中间件将无法获得路由信息。
路由信息对于某些中间件非常有用,比如CORS、认证中间件(认证过程可能会用到路由信息)。
同时端点路由提炼出端点概念,解耦路由匹配逻辑、请求分发。
Endpoint Routing中间件
由一对中间件组成:
UseRouting 将路由匹配添加到中间件管道。该中间件查看应用程序中定义的端点集合,并根据请求选择很好匹配。UseEndpoints 将端点执行添加到中间件管道。
MapGet、MapPost等方法将 处理逻辑连接到路由系统;
其他方法将 ASP.NET Core框架特性连接到路由系统。
- MapRazorPages for Razor Pages
- MapControllers for controllers
- MapHub< THub> for SignalR
- MapGrpcService< TService> for gRPC
处于这对中间件上游的 中间件: 始终无法感知 Endpoint;
处于这对中间件之间的 中间件,将会感知到Endpoint,并有能力执行附加处理逻辑;
UseEndpoint是一个终点中间件;
没有匹配,则进入UseEndpoint之后的中间件。
放置在UseRouting、UseEndpoints之间的认证授权中间件可以:
感知被匹配的端点信息;在调度到Endpoint之前,应用授权策略。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Matches request to an endpoint.
app.UseRouting();
// Endpoint aware middleware.
// Middleware can use metadata from the matched endpoint.
app.UseAuthentication();
app.UseAuthorization();
// Execute the matched endpoint.
app.UseEndpoints(endpoints =>
{
// Configure the Health Check endpoint and require an authorized user.
endpoints.MapHealthChecks("/healthz").RequireAuthorization();
// Configure another endpoint, no authorization requirements.
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
} 新闻标题:Core端点路由在ASP.NET中的作用有哪些-创新互联
本文URL:http://www.jxjierui.cn/article/csccsc.html


咨询
建站咨询
