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 provided link account is a controller, login is action method and 24 is parameter or query string.
Let’s review the different ways to build routing in ASP.NET CORE.
Creating Default Routes
By convention in your project’startup class, the default route can be defined.
We need to be sure that essential configuration for MVC pattern of Controller + Action + ID route, exist in our project. Routing pattern can also be declared like:
Extending Default Routes
On specific needs, Default Route can be extended by adding customized routes. By MapRoute() method, configurations can be added:
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.
Using attributes in controller and actions, you can also configure routes.
Now, controller actions can be accessed through the following routes:
Two tokens [controller] and [action] indicate that we have to refer to the controller and action name that has been declared. In this case, “Analytics” is the name of the controller, and “Charts” the name of the action, therefore it the name of the route.
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, HttpDelete attributes
Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.