Routing in Microsoft MVC or WebAPI applications serves as a map to define the routes for all incoming requests. There are three basic components of routing in MVC & WebAPI applications Controller, Action method, and parameters.
In the above URL, account is a controller, login is the action method and 24 is a parameter/query string.
Now, Let’s go through different ways to build routing in ASP.NET CORE.
Routing in MVC by Creating Default Routes
By convention, you can define the default route in your project’s startup class.
Make sure that essential configuration for MVC pattern of Controller+Action+ID routes exist in the project. In addition, you can also declare routing pattern like this:
Routing in MVC by Extending Default Routes
On specific needs, you can extend a default route by adding customized routes. For this, add a configuration by using the MapRoute() method:
On the Home controller, adding extra route allows access to About action with an/about the route. About page can be still accessed with conventional/home/about the route as the default pattern route is still present.
Routing in MVC by Using Attributes
Additionally, you can also configure routes using attributes in controller and actions.
Now, you can access controller actions through the following routes:
Two tokens [controller] and [action] show that we have to refer to the declared controller and action name. In this route, “Analytics” is the name of the controller, and “Charts” the name of the action.
By Building RESTful Routes
We need to use the following route configuration to declare a RESTful controller.
Here, RESTful service is told to accept calls under the /api/values route. Now, we do not use the Route attribute for actions. Instead, we decorate it with HttpGet, HttpPost, HttpPut, and HttpDelete attributes.
Do you think this was a useful article for you? Feel free to provide your comments and concerns in the below comment section.