Asp.net Core MVC (three) UseMvc routing settings

Work from home, from work to continue and see everything, no nonsense, continue to look at the routing MVC.

asp.net mvc routing core is built on top of the core routing asp.net. Load routing middleware endpoint configuration by not elaborate on this, ( DOTNET Core MVC (b) already described ). In a look at other ways to load:

app.UseMvc (routes => 
{ 
  // . with the specified name and templates will be added to the route IRouteBuilder 
   routes.MapRoute ( " default " , " {Home} = the Controller / Action = {Index} / {the above mentioned id}? " ); 
});

In this way the .net core 3.0 (using endpoint load routing middleware) will be prompted to use

 

 

We follow the prompts to add in the code:

ConfigureServices method added:

// not enabled endpoints 
services.AddMvc (Options => options.EnableEndpointRouting = false );
public static IApplicationBuilder UseMvc( this IApplicationBuilder app, Action<IRouteBuilder> configureRoutes
{
    if (app == null)
    {
        throw new ArgumentNullException(nameof(app));
    }
    if (configureRoutes == null)
    {
        throw new ArgumentNullException(nameof(configureRoutes));
    }
    //在调用UseMvc之前验证AddMvc是否已完成
    VerifyMvcIsRegistered(app);
    var options = app.ApplicationServices.GetRequiredService<IOptions<MvcOptions>>();
    if (options.Value.EnableEndpointRouting)
    {
        var message =
            "Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use " +
            "'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside " +
            "'ConfigureServices(...).";
        throw new InvalidOperationException(message);
    
        the DefaultHandlerRouteBuilder (App)new newroutes =varRouteBuilder Router creates the required intermediate object RouterMiddleware@create a default handler class mvc 
    //
    }
     
    {App.ApplicationServices.GetRequiredService = <The MvcRouteHandler> (), 
    }; 
    // configuration MVC route callback 
    configureRoutes (routes);
     // CreateAttributeMegaRoute: Returns a IRouter mainly used to process RouteAttribute mark the Action, 
    routes.Routes.Insert ( 0 , AttributeRouting.CreateAttributeMegaRoute (app.ApplicationServices));
     // used to develop middleware routing field to route the braking ApplicationBuilder 
    return app.UseRouter (routes.Build ()); 
}
public  interface IRouteBuilder 
{ 
    // Get applictionbuilder (middleware added to the delegate application request pipeline) 
    IApplicationBuilder ApplicationBuilder { GET ;}
     // Get Routing (core) 
    IRouter the DefaultHandler { GET ; SET ;}
     // Get IServiceProvider route to parse a set of services 
    the IServiceProvider ServiceProvider { GET ;}
     // Get route set 
    the IList <IRouter> the routes { GET ;} 
    IRouter the Build (); 
}
//主要是用来处理 RouteAttribute 标记的Action,
public static IRouter CreateAttributeMegaRoute(IServiceProvider services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }
    return new AttributeRoute(
        services.GetRequiredService<IActionDescriptorCollectionProvider>(),
        services,
        actions =>
        {
            var handler = services.GetRequiredService<MvcAttributeRouteHandler>();
            handler.Actions = actions;
            return handler;
        });
}

First wrote here, although it used the method over again, but still way flow of the entire route is not very clear, so we will detail the next pipeline transfer process the following route.

 Created, reproduced indicate the source.
 

 

 

 

Guess you like

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