Routing for ASP.NET MVC

We just created an ASP.NET MVC controller and added a view for an Index action in the controller. Now we need to configure routing for the ASP.NET MVC application so that any request to the Index action is handled by the ASP.NET MVC router. Follow these steps to configure ASP.NET MVC routing:

  1. Open Startup.cs.
  2. Comment or remove the app.UseDefaultFiles() statement from the Configure method as we will serve views using ASP.NET MVC.
  3. Replace the app.UseMvc() statement in the Configure method with this one:
     app.UseMvc(config =>   
            {   
              config.MapRoute(   
              name: "Default",   
              template: "{controller}/{action}/{id?}",   
     defaults: new   { controller = "Home", action =    
"Index" } ); });

Here, we have added the default routing for ASP.NET MVC. Any request to the Web API is mapped with the HTTP verbs or actions in the controller.

Let's run the application by pressing F5, and you will get the illustrated screen rendered in the browser:

ASP.NET MVC HomeController Index View rendered in browser
..................Content has been hidden....................

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