【发布时间】:2019-09-08 22:13:15
【问题描述】:
当我试图理解它们时,它们似乎都用于将请求路由/映射到某个端点
【问题讨论】:
-
它在文档中。参考Routing in ASP.NET Core
标签: c# asp.net-core asp.net-core-mvc
当我试图理解它们时,它们似乎都用于将请求路由/映射到某个端点
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc
UseRouting:将请求匹配到端点。
UseEndpoints:执行匹配的端点。
它将路由匹配和解析功能与端点执行功能分离,到目前为止,这些功能都与 MVC 中间件捆绑在一起。
这使得 ASP.NET Core 框架更加灵活,并允许其他 UseRouting 和 UseEndpoints 之间的中间件。这允许 那些中间件利用来自端点路由的信息, 例如,对 UseAuthentication 的调用必须在 UseRouting,以便在 UseEndpoints 之前路由信息可用于身份验证决策,以便用户 在访问端点之前进行身份验证。
【讨论】:
基于 dotnet 核心文档:
╔══════════════════════════════════════════╦═══════════════════════════════════════╗
║ app.UseRouting() ║ app.UseEndPoints() ║
╠══════════════════════════════════════════╬═══════════════════════════════════════╣
║ Find Endpoint ║ Execute Endpoint ║
║ ║ ║
║ Adds route matching to the middleware ║ Adds endpoint execution to the ║
║ pipeline. This middleware looks at the ║ middleware pipeline. ║
║ set of endpoints defined in the app, ║ It runs the delegate associated ║
║ and selects the best match based ║ with the selected endpoint. ║
║ on the request. ║ ║
║ ║ ║
╚══════════════════════════════════════════╩═══════════════════════════════════════╝
根据上表我们应该注意一些提示:
如果应用调用 UseStaticFiles,请将 UseStaticFiles 放在 UseRouting 之前。
务必将 Authentication 和 Authorization 中间件放在 UseRouting 和 UseEndPoints 之间。
在 UseRouting() 调用之后出现的任何中间件都将知道最终将运行哪个端点。
在 UseRouting() 调用之前出现的任何中间件都不知道最终会运行哪个端点。
【讨论】:
place the Authentication and Authorization middleware between UseRouting and UseEndPoints 有什么特别的原因吗?
UseRouting 和UseEndpoints 之间。 UseAuthentication 是一个不同的故事(你的链接放在更早的地方)。
首先,你可以看看他们的source code:
1.使用路由
public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
VerifyRoutingServicesAreRegistered(builder);
var endpointRouteBuilder = new DefaultEndpointRouteBuilder(builder);
builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder;
return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder);
}
2.使用端点
public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action<IEndpointRouteBuilder> configure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
VerifyRoutingServicesAreRegistered(builder);
VerifyEndpointRoutingMiddlewareIsRegistered(builder, out var endpointRouteBuilder);
configure(endpointRouteBuilder);
// Yes, this mutates an IOptions. We're registering data sources in a global collection which
// can be used for discovery of endpoints or URL generation.
//
// Each middleware gets its own collection of data sources, and all of those data sources also
// get added to a global collection.
var routeOptions = builder.ApplicationServices.GetRequiredService<IOptions<RouteOptions>>();
foreach (var dataSource in endpointRouteBuilder.DataSources)
{
routeOptions.Value.EndpointDataSources.Add(dataSource);
}
return builder.UseMiddleware<EndpointMiddleware>();
}
然后参考No overload for method 'UseRouting' takes 1 arguments详细解释它们之间的区别。
ASP.NET Core 3 使用改进的端点路由,通常会 对应用程序内的路由提供更多控制。端点 路由分为两个独立的步骤:
在第一步中,请求的路由与配置的路由再次匹配 路线来确定正在访问的路线。
在最后一步, 正在评估确定的路线和相应的中间件, 例如MVC,被调用。
这两个步骤由 app.UseRouting() 和 app.UseEndpoints() 设置。前者将注册运行逻辑以确定路由的中间件。然后后者将执行该路由。
另外,参考
https://asp.net-hacker.rocks/2019/08/12/aspnetcore30-look-into-startup.html https://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/
【讨论】:
UseRouting 计算应为请求 URL 路径使用的路由,但不会在管道中的这一点进行路由。 UseRouting 添加了可供后续中间件使用的元数据。
UseEndpoints 执行 Controller 和相应的处理程序。
看看这篇有用的文章:https://andrewlock.net/converting-a-terminal-middleware-to-endpoint-routing-in-aspnetcore-3/
【讨论】:
app.UseRouting()
标记中间件管道中做出路由决策或请求与端点匹配的位置。换句话说,端点被选择的地方。
app.UseEndPoints()
标记中间件管道中执行所选端点的位置。它执行端点。
【讨论】:
app.UseRouting :
UseRouting 将路由匹配添加到中间件管道。该中间件查看应用程序中定义的一组端点,并根据请求选择最佳匹配。
app.UseEndpoints:
UseEndpoints 将端点执行添加到中间件管道。它运行与所选端点关联的委托。
端点可以是:
应用可以匹配和执行的端点在UseEndpoints中配置
【讨论】: