首页 新闻 会员 周边

ASP.NET Core 3.0 中 app.UseRouting 与 app.UseEndpoints 的区别是什么

1
悬赏园豆:30 [已解决问题] 解决于 2019-05-25 13:36

在 ASP.NET Core 3.0 需要下面这样配置路由,请问 app.UseRouting 与 app.UseEndpoints 的区别是什么?

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});
问题补充:

如果去掉 app.UseRouting() 访问时会抛下面的异常

System.InvalidOperationException: EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code.
dudu的主页 dudu | 高人七级 | 园豆:31003
提问于:2019-05-16 11:01

默认路由简洁写法: app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());

dudu 4年前
< >
分享
最佳答案
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);
        }
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>();
        }

来源: https://github.com/aspnet/AspNetCore/blob/master/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs

收获园豆:30
BUTTERAPPLE | 老鸟四级 |园豆:3190 | 2019-05-16 11:22

BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2019-05-16 11:34

@BUTTERAPPLE: 要的就是这句

The two steps are set up by app.UseRouting() and app.UseEndpoints(). The former will register the middleware that runs the logic to determine the route. The latter will then execute that route.

dudu | 园豆:31003 (高人七级) | 2019-05-25 13:37
其他回答(2)
0

route是遍历找到一个合适的action,endpoint省了遍历这一步, 记得好像是这样子,在netcore的blog的介绍上好像有看到过。

czd890 | 园豆:14412 (专家六级) | 2019-05-16 11:07
0

前者是根据当前请求找到endpoint,后者是拿到上面找到的endpoint去执行请求的最终处理, 其实在这个之间可以加入自己的中间件去做别的处理,而且这个中间件也可以拿到上面匹配得到的Endpoint。终节点路由的目的也是这样,让后续的中间件可以访问本次请求对应的终节点,所谓的终节点可以理解为最终要执行的那个Action

变形精怪 | 园豆:5 (初学一级) | 2019-12-04 15:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册