【问题标题】:What are the differences between app.UseRouting() and app.UseEndPoints()?app.UseRouting() 和 app.UseEndPoints() 有什么区别?
【发布时间】:2019-09-08 22:13:15
【问题描述】:

当我试图理解它们时,它们似乎都用于将请求路由/映射到某个端点

【问题讨论】:

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

UseRouting:将请求匹配到端点。

UseEndpoints:执行匹配的端点。

它将路由匹配和解析功能与端点执行功能分离,到目前为止,这些功能都与 MVC 中间件捆绑在一起。

这使得 ASP.NET Core 框架更加灵活,并允许其他 UseRoutingUseEndpoints 之间的中间件。这允许 那些中间件利用来自端点路由的信息, 例如,对 UseAuthentication 的调用必须在 UseRouting,以便在 UseEndpoints 之前路由信息可用于身份验证决策,以便用户 在访问端点之前进行身份验证。

【讨论】:

    【解决方案2】:

    基于 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.                         ║                                       ║
    ║                                          ║                                       ║
    ╚══════════════════════════════════════════╩═══════════════════════════════════════╝
    

    根据上表我们应该注意一些提示:

    1. 如果应用调用 UseStaticFiles,请将 UseStaticFiles 放在 UseRouting 之前。

    2. 务必将 AuthenticationAuthorization 中间件放在 UseRouting 和 UseEndPoints 之间。

    3. UseRouting() 调用之后出现的任何中间件都将知道最终将运行哪个端点。

    4. UseRouting() 调用之前出现的任何中间件都不知道最终会运行哪个端点。

    【讨论】:

    • place the Authentication and Authorization middleware between UseRouting and UseEndPoints 有什么特别的原因吗?
    • @AlexKlaus 请阅读此answer
    • 它只提到UseAuthorization 确实有记录要求出现在UseRoutingUseEndpoints 之间。 UseAuthentication 是一个不同的故事(你的链接放在更早的地方)。
    • 我认为认证中间件不需要路由信息thisthis如果在路由之前做认证是没有问题的,可能另一方面如果做了这个改动,所有的请求将被认证,无论路由是否正确
    【解决方案3】:

    首先,你可以看看他们的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/

    【讨论】:

    • 多么糟糕的设计,我们必须深入到框架的代码中才能弄清楚这一切意味着什么。
    【解决方案4】:

    UseRouting 计算应为请求 URL 路径使用的路由,但不会在管道中的这一点进行路由。 UseRouting 添加了可供后续中间件使用的元数据。

    UseEndpoints 执行 Controller 和相应的处理程序。

    看看这篇有用的文章:https://andrewlock.net/converting-a-terminal-middleware-to-endpoint-routing-in-aspnetcore-3/

    【讨论】:

      【解决方案5】:

      app.UseRouting()

      标记中间件管道中做出路由决策或请求与端点匹配的位置。换句话说,端点被选择的地方。

      app.UseEndPoints()

      标记中间件管道中执行所选端点的位置。它执行端点。

      【讨论】:

        【解决方案6】:

        app.UseRouting :

        UseRouting 将路由匹配添加到中间件管道。该中间件查看应用程序中定义的一组端点,并根据请求选择最佳匹配。

        app.UseEndpoints:

        UseEndpoints 将端点执行添加到中间件管道。它运行与所选端点关联的委托。

        端点可以是:

        1. 已选中,通过匹配 URL 和 HTTP 方法。
        2. 通过运行委托来执行。

        应用可以匹配和执行的端点在UseEndpoints中配置

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-14
          • 2021-12-17
          • 2010-10-02
          • 2011-12-12
          • 2010-09-16
          • 2012-03-14
          • 2012-02-06
          • 2011-02-25
          相关资源
          最近更新 更多