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 technique to create the default route. Let's divide this code into its smaller pieces.

The UseMvc method retrieves a single parameter, which is a callback for configuring routes:

app.UseMvc(routes => { 
// ... route configuration ...
}

The routes parameter that is passed into the callback is of the IRouteBuilder type, which contains different methods for configuring routes. We will look at the most commonly used one, MapRoute.

The MapRoute method gets a few parameters, the basic ones of which are the route name and its URL pattern:

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

The route name does not affect URL matching. It becomes handy in different scenarios where you need to create a URL within code based on a route. Route names must be unique throughout the application, otherwise an exception will be thrown on application startup.

The route template is the URL pattern for the route. This pattern will be used by the ASP.NET Core MVC router when matching incoming request URLs to routes. It can contain default values as well.

In the preceding code block, the "{controller=Home}/{action=Index}/{id?}" template stores a few techniques.

Firstly, the stripped down version of this template will be "{controller}/{action}", which tells ASP.NET Core MVC that the first part of an incoming request URL matches a controller name, such as Home, and the second part matches an action name, such as Index. Therefore, a request to http://example.com/Home/Index will end up executing the Index action method within the HomeController class. The controller and action elements within URL templates are called tokens.

Secondly, the {token=value} syntax defines a default value in case the URL is missing it. In our case, {controller=Home}/{action=Index} means that if the incoming URL is http://example.com, then ASP.NET Core MVC will execute the Index method within the HomeController class. If the incoming URL is http://example.com/Users, the Index method within the UsersController will be executed.

Thirdly, the last part of the template is {id?}—this tells the router that a parameter will optionally be added to the URL. Once that happens, this parameter will be sent to the matching action as a method parameter. For example, if the incoming URL is http://example.com/Users/Edit/5, the Edit method within the UsersController class will be executed and 5 will be sent to it as a parameter. The question mark (?) at the end means that this is an optional parameter—if we remove it, then URLs will be required to contain an ID at the end, or the route will not match.

This route is the default route for ASP.NET Core MVC because it catches all URLs that match the MVC convention of controller/action/id. We can also add more specific routes that do not necessarily follow this convention and set the matching controller and action method via the defaults parameter. For example, the following route will result in requests for http://example.com/hello being directed to the Index method of the HomeController:

routes.MapRoute(
name: "hello",
template: "hello",
defaults: new { controller = "Home", action = "Index" });

Make sure that routes specified within UseMvc are matched by their order of definition. To do this, it is necessary to define the more specific routes before the less specific ones. For example, taking our previous code block, the full UseMvc code looks as follows:

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

Conventional routing makes it easier to find out all the routes in the system and enables sophisticated route patterns, but also makes it harder to maintain as the application gets bigger. This is, usually, the preferred technique for ASP.NET Core MVC applications.

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

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