ASP.NET MVC Routing

Beginner

2019-04-10

ASP.NET MVC Routing

MVC applications use the ASP.NET routing system, which decides how URLs map to controllers and actions. When Visual Studio creates the MVC project, it adds some default routes to get us started. You can request any of the following URLs, and they will be directed to the Index action on the HomeController:
/
/Home
/Home/Index

ASP.NET MVC Route Engine

Why Use Routing ?

one of the benefits using MVC Routing is we can convert ugly and long URLs to Hackable and user friendly URLs optimized for SEO.

that means we can convert this URL :

www.example.com/article.aspx?id=69&title=my-favorite-post

to

www.example.com/articles/69/my-favorite-post

The Route Table

The Route Table is a collection of all possible routes that MVC can use to match submitted URLs.

Any URL that is submitted to the application will be compared to the routes in the Route Table, and the system will redirect to the first matching route found in that table.

you can see how routing table is registered through two steps :

open Global.asax file from solution explorer

Global Asax File

you can see RouteConfig.RegisterRoutes(RouteTable.Routes); in Application_Start() method


open the RouteConfig.cs in App_Start folder


route.MapRoute() registers a new routing convention

the default routing convention as you can see in the code above corresponds to controller : Home and Action Index with URL Home/Index or /Home or / with id as an optional parameter

that means if we navigated to

http://localhost:50803/ or http://localhost:50803/Home or http://localhost:50803/Home/Index they all will navigate to the same controller with the same action method

Adding New Route

in RegisterRoutes() method we can register new routes by specifying their convention

for example, if we want to add a new URL convention to map to the same controller and action we created before modify it to be :


run the project and navigate to http://localhost:50803/Home or http://localhost:50803/Alternative , you can find both are redirecting to the same controller and action

MVC Routing

Parsing Parameters

In the default route a parameter is passed called id. Let’s see how to utilize this parsing.

Modify the Index action method to be :


run the project and navigate to http://localhost:50803/Home/Index/87

asp.net-mvc-controller-parameters

Enabling Attribute Routing

we have seen how to modify the routing table with routing convention . you can use routing attributes to make custom routing URLs.

you have to enable it by calling MapMvcAttributeRoutes on the RouteCollection for your app (usually this is done in RouteConfig)

modify the RegisterRoutes method to be like this :


Then modify the class to add the routing attribute called “Attribute” as below


now if you run the project and navigate to http://localhost:50803/Attribute

asp.net-mvc-routing-attribute

That means if we navigate to the root URL or /Attribute they both will map the Home controller

Summary

In this tutorial we studied what are Routes and how they are registered and altered . we also parsed an optional id parameter to the Index action allowing us to make more interaction with our MVC classes.