(B) Preliminary routing DOTNET Core MVC

Set aside a few days, busy mess, finally taking the time today to continue to look at MVC knowledge. Let's look at how the MVC routing is handled. The following is an alternative route:

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

At runtime, the registration form through the Configure method delegate.

UseEndpoints : The middleware is added to the IApplicationBuilder then call this method, you need to call UseRouting.

public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
{
    if (builder == null)
    {
        throw new ArgumentNullException(nameof(builder));
    }
    VerifyRoutingServicesAreRegistered (Builder); // check whether the loaded 
    var endpointRouteBuilder = new new DefaultEndpointRouteBuilder (Builder);
    builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder;
    return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder);
}

Adding Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware middleware to the specified IApplicationBuilder in.

private static void VerifyRoutingServicesAreRegistered(IApplicationBuilder app)
{
    // Verify if AddRouting was done before calling UseEndpointRouting/UseEndpoint
    // We use the RoutingMarkerService to make sure if all the services were added.
    if (app.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null)
    {
        throw new InvalidOperationException(Resources.FormatUnableToFindServices(
            nameof (IServiceCollection),
            nameof(RoutingServiceCollectionExtensions.AddRouting),
            "ConfigureServices(...)"));
    }
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>();
}

ASP.NET Core 3 using a sound endpoint routing , it can usually internal application routing provides more control. Endpoint routing into two separate steps:

In the first step, again matching routes in the routing and configuration requested to find out is to visit the route. In the last step, to determine the route to assess, and call the appropriate middleware.

Two separate steps, allowing other intermediate function between these points. Middleware allows routing information from the endpoint to a sub-authorization process, without having to perform an actual processing program.

app.UseRouting () will run routes in the routing logic registration middleware.

app.UseEndpoints () to perform the routing.

MapControllerRoute : Add the endpoint controller operation IEndpointRouteBuilder  and specify a given route name, pattern,   defaults, constraints, and dataTokens.

other:

MapController added to the attribute controller supports routing.

MapController be added to the controller support this attribute of the route.

MapControllerRoute add controllers conventional route

Original. Please indicate the source.

A bunch of long-winded, said the two methods, the next routing usage went on to say -

 

Guess you like

Origin www.cnblogs.com/xtt321/p/12340001.html