ASP.NET Core MVC Routing – Session 12

In this session of ASP.NET Core Tutorial we explain ASP.NET Core MVC Routing in detail. Also we will implement several scenarios to make this topic as clear as possible.

How ASP.NET Core MVC Routing works?

Generally, routing in ASP.NET Core MVC is working based on the pattern that we implement within UseEndpoints middleware. Following to default MVC pattern, first section after domain/webserver name is pointing to Controller name. The second section is related Action Method name and thired part which is optional “id” is related to passing a value to called action method.

Routing Pattern

Routing in ASP.NET Core MVC, is based on the pattern that we entered within UseEndpoints middleware, if Endpoint Routing is activated. Also, if you are using earliest version of ASP.NET Core, routing is working based on UseMvcWithDefaultRoute middleware or the pattern that we insert within UseMVC middleware. The default routing pattern is {Controller=home}/{Action=index}/{id?}. In this pattern, after domain name or webserver name we should mention Controller name. Then, the next part is the place that we should mention name of our Action Method within selected Controller.

Moreover, we can pass a value to the selected Action Method with “id” part of our URL which is optional. Thus, based on the default pattern, if we don’t enter Controller and Action Method name, the web app is looking for Index action method within Home controller to be presented. It is obvious if the mentioned controller and action method is not exist, 404 page error will appear which is related to Not Found requested resource.

ASP.NET Core Routing sample

Changing Default Route

You can change default controller and action method by changing Controller name and Action Method name in Pattern after equal sign. Also you can make an argument optional by adding question mark (?) to end of its name, same as “id” in default routing pattern.

For instance, to make Cost Controller and Detail Action method as a default route, you should change default routing to {Controller=Cost}/{Action=Detail}/{id?}. Moreover, Action Method can have access to value of “id” through “id” parameter in respective Action method public string detail(int id) .

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube and you can have access to list of all sessions HERE.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes


0 0 votes
Article Rating

Inline Feedbacks
View all comments