What are the differences between app.UseRouting() and app.UseEndPoints()?

后端 未结 5 894
忘掉有多难
忘掉有多难 2021-01-31 14:48

As I\'m trying to understand them, It seem like they are both used to route/map the request to a certain endpoint

5条回答
  •  有刺的猬
    2021-01-31 14:56

    First of all,you could have a look at their source code:

    1.UseRouting

    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(endpointRouteBuilder);
        }
    

    2.UseEndPoint

    public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action 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>();
            foreach (var dataSource in endpointRouteBuilder.DataSources)
            {
                routeOptions.Value.EndpointDataSources.Add(dataSource);
            }
    
            return builder.UseMiddleware();
        }
    

    Then refer to No overload for method 'UseRouting' takes 1 arguments which explains the differences between them in detail.

    ASP.NET Core 3 uses a refined endpoint routing which will generally give more control about routing within the application. Endpoint routing works in two separate steps:

    In a first step, the requested route is matched agains the configured routes to figure out what route is being accessed.

    In a final step, the determined route is being evaluated and the respective middleware, e.g. MVC, is called.

    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.

    Also, refer to

    https://asp.net-hacker.rocks/2019/08/12/aspnetcore30-look-into-startup.html https://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/

提交回复
热议问题