Routing in ASP.NET MVC

ASP.NET MVC routing maps the request to the controller actions. All routes will be defined in the route table and are used by the route engine to match the URL patterns of the requests with the controllers and actions.

We can add the routes to the route table in the configure method of the Startup.cs file. The following code snippet shows the default route registered on the route table:

public void Configure(IApplicationBuilder app) 
{ 
    app.UseIISPlatformHandler(); 
    app.UseDefaultFiles(); 
    app.UseStaticFiles(); 
    app.UseMvc(config => 
    { 
        config.MapRoute( 
            name: "Default", 
            template: "{controller}/{action}/{id?}", 
            defaults: new { controller = "Home", action = "Index" } 
            ); 
    });             
} 

Here, a route is registered with a template and defaults. If there is no controller or action name provided in the URL, the request will be mapped to the Index action in the HomeController class; otherwise, it will be mapped to the respective controller action.

In our application, we have three MVC controllers, namely, HomeController, UserController, and TodoController.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.183