Adding routes via UseMvc

Adding routes via the UseMvc method, also known as the conventional way of adding MVC routes, is a way of defining all routes in a central location.

The UseMvc method can be found within the Startup.cs file within the Configure method, which we talked about earlier in this chapter. At the moment, the following line will be present:

app.UseMvcWithDefaultRoute();

This tells ASP.NET Core MVC to create a default route that interprets incoming request URLs according to the MVC convention. Let's change this to its explicit equivalent:

app.UseMvc(routes =>{    routes.MapRoute(        name: "default",        template: "{controller=Home}/{action=Index}/{id?}");});   

In the preceding code snippet, we are using the conventional route creation ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.