Traditionally, in any web application, URLs are mapped to a physical location of the specified page in the virtual directory created on web server. But in MVC we don’t have specific physical files to be mapped for different page requests, so routing mechanism is used in MVC to decide that which action method of which controller class needs to be executed for the incoming browser request. The URL in MVC looks like http://mybookstrore.com/Book/Index/1 , here the Index method of the ‘Book controller’ will be executed by passing book id 1 as a parameter.

Routes are defined in the global.asax file and processed at the application’s startup when Application Start function is invoked. Inside this method, one call is made to the RegisterRoutes method of the RouteConfig class placed inside App_Start folder of the project.

 

The default MVC project template comes with a Default pre-configured route as below. This route can handle any request that has a maximum of 3 segments ~/ {controller}/ {action}/ {id}.

 

Using custom routes

It is not necessary to use default pre-configured route only, we can create our own custom routes in the same route collection. The custom routes should be written always before the default route because the order in which we register the routes is followed by the routing engine and if we place the default route before the custom route, then default route will be called every time and custom route will never get called.

In the below example route, the route name is ‘Library’ and the controller name is ‘Book’ that will handle the request. Default action name is ‘Details’ and the passed parameter name is bookId. If the URL like http://mybookstore.com/Student/1  will be requested, then this new route named ‘Library’ will be called instead of Default route and ‘Details’ action of Book Controller will be executed passing 1 in the bookId.

Custom routes by User Role

 

If we want to use different routes for different user roles like admin, student etc., then we can register different routes with a prefix in the route URL to track the user role requesting the URL. For Ex: http:// mybookstore .com/Admin/Home/Index

URL rewriting

Suppose if our MVC application is publically available and any user has bookmarked the site URL http://localhost/Book/index. Now if the controller name in the application gets changed from BookController to BKController. then the new URL will be http://localhost/BK/index and the old bookmarked URL will not work for users of the application. To overcome this, we can define a route in the RouteConfig.cs file to re-write the URL as below:

 

Here we’ve statically defined the controller name as “Book” and dynamically specified the Controller value as “BK”. After adding this route, the old URL will also work as is and we will get the result without any error.

Route Constraints

 

When we create a route, we can add constraints in that. For example, we can use below route constraint to limit the parameter bookId value to accept integer values only.

If the parameter value in any route does not satisfy the constraint, then the route is not matched by the routing engine and it continues searching for the next matching route which would otherwise throw an exception if the constraint is not specified and wrong argument value is passed.

So, here we can say that by routing we can make URLs more descriptive for the user’s action and henceforth are more easily understood by the users. MVC 5 introduced ‘Attribute Routing’, where the route can be defined on top of the controller action method. ‘Attribute Routing’ gives us more control over the URIs in our MVC web application, which I will be discussing in detail in my next blog.

Posted by Advaiya

    Let's connect to make technology work for you.





    Please tick the options most relevant to your business challenges
    Decision makingBusiness productivityCustomer experienceTechnology-led innovationDigital transformation


    [By using this form you agree with the storage and handling of your data by this website.]