Routing In MVC

Introduction

There exists an assumption that there is a direct relationship between the requested URLs and the files stored on the server. But does this make sense in an MVC application, where requests are processed by action methods and controllers and there is no relation to files on the server (disk). MVC applications use routing system in order to track the request and give response to the users. The routing system helps create patterns of URLs, that too pattern of our wish. The routing system does the following job, incoming URL examination. It checks the url requested and it intends the controller and action and based on the url, it redirects the controller based on the client request. This is a very interesting and an important subject of discussion. Once we create an MVC application, it is already configured to use ASP.NET routing system. There are four sections that are any way required relevantly for the routing system in the application. They are the following:

  • system.web httpModules: These are required to configure the modules within an application. Http Modules are assemblies that are called on every request made to the application.

  • system.web.httpHandlers: This runs in response to the request made to the application.
The above mentioned are required for the routing system and should be present in the configuration of the application. We need to make sure the above configurations exist in our application.

Let's start with snippets
  1. public class RouteConfig {  
  2.     public static void Registerroutes (RouteCollection routes) {  
  3.          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.          routes.MapRoute (  
  5.             name : "Default",  
  6.             url: "{controller}/{action}/{id}",  
  7.             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  8.     );  
  9. }  
Now, this RouteConfig.cs resides inside the App.Config folder. The RegisterRoutes method inside the RouteConfig.cs gets called from the heart of the MVC application i.e. Global.asax.cs, which sets the core MVC features when the application is started. The called RegisterRoutes method gets called inside Global.asax.cs as follows:
  1. RouteConfig.RegisterRoutes(RouteTables.Routes);  
To the method as we can see the parameter is passed i.e. RouteTables.Routes which is a collection routes or the instance of the RouteCollection.cs. To register adding a new route, we need to register inside the RegisterRoutes method as below:
  1. Route demoRoute = new Route("{controller}/{action}"new MvcRouteHandler());  
  2. routes.Add("DemoRoute", demoRoute);  
Let's understand the above snippet, here a new route named demoRoute is being created. MvcRouteHandler() is also passed to the constructor. This extends the IHttpHandler and passes the request routes, the object above is an instance of the RouteCollection class which is passed from the Global.asax.cs. There is another easy way of registering routes, i.e. by using MapRoute method defined by RouteCollection class like the following code snippet:
  1. routes.MapRoute("RouteName", "{controller}/{action})    
One thing to note here is, if the route in case do not match any of the routes in the RouteCollection, then the error is thrown to the user. Generally, default route is set to Index as action. Then, the url with http://demosite/home will also get the user to the Index action method. If both the controller and action are provided to the routes, then routes match url with no segments (if default is defined in the RouteConfig). Generally, the controller if only used in the url, then the Controller's action Index method is called i.e.
http://demosite.com/Admin
This would call the Admin Index method. If any other action method is called, then similarly based on the segments in the url requested, the action in the specified controller is called.

Static URL Segments

There is this concept of static url segments. This is rarely used but the concept is quite interesting. Here, the route is registered inside RouteConfig like the following code snippet:

  1. routes.MapRoute(""), "X{controller}/{action}");  
Here, what happens is whatever is present after X or suffixed to X, that controller and action gets called. For example, http://demosite.com/XAdmin/Index, here what happens is the controller Admin and the Action Index is called. There is a concept of Route Ordering as well, in which in the order the routes are defined in the RouteCollection, the same order the route mapping is done. I.E. when a URL request is made to the application, the route config maps the route defined until a match is found in the same order routes are defined. Another interesting fact to notice here. Suppose we have a controller named "Admin" and sometime in the future we require to modify the name of that controller. Now if we do not modify our RouteConfig, we would get a 404 NOT FOUND error. To avoid this we do the following modification in the Route.Config.cs: 
  1. public static void RegisterRoutes (RouteCollection routes) {  
  2.  routes.MapRoute ("Admin","Admin/{action}",  
  3.     new {controller="Home"});  
  4. }  
Thus, when a request as /Admin/Index comes to the server, the controller called is /Home/Index, avoiding the 404 error page. Since, there is no segment variable supplied, so default provided is used. The same can be done in case of an Action as well, i.e. if an action is removed from the controller, we can have a default route check for that action as well and redirect to the specified and default route URL if not found. Like as below:
  1. routes.MapRoute("Admin","Admin/department",  
  2.   new {controller = "Admin",action = "Index"});  
Defining Custom Segment Variables

Controller and Action are common part of any url request to the MVC application and these would be considered as the in-built segment variables. But their are other segment variables that can be customized for the service requests. Let's take a look at the following snippet:
  1. public static void RegisterRoutes(RouteCollection routes) {  
  2.     routes.MapRoute("RouteDemo""{controller}/{action}/{id}")  
  3.         new {controller = "Admin" , action="GetDept", id = "DefaultID"});  
  4. }  
Here we can see we have declared a custom varialble "id" with a default value. Thus any url request with two or three segment variables would work out. If the id is not specified explicitly in the URL, then it takes the default id value. Usually, the third segment variable is used to fetch the parameter to be utilized in the service methods.
  1. public ActionResult GetDepartment(int id) {  
  2.   //Used id to get the particular department.  
  3. }  
Here if id is not provided in the URL, then it searches using default id specified and may return null. Thus, to avoid, we can also define the third segment variable to be optional, so that we have action to also return result if no id is specified in the URL.
  1. routes.MapRoute("OptionalRoute""{controller}/{action}/{id}",  
  2.      new {controller = "Admin", action = "GetDept", id = UrlParameter.Optional });  
Thus the UrlParameter.Optional makes the third segment variable optional and rund through without any default variables as well.

Prioritizing Controllers with same name using name spaces

This is a very interesting and strong concept which can be really handy, when working on a huge MVC application. Lets take a scenario to understand this, suppose we have a controller name "Home "inside the controllers folder and another controller with same name "Home" inside another folder say "AddonControllers". So when we run the project and hit the URL /Home/Index, then the routing and mapping that searches the URL requested, will be in ambiguity as it fetches two controllers with same names. Thus this will throw an error with the exception message as:

Multiple types were found that match the controller named "Home". This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify the namespace to search for a controller that matches the request..
Thus, from the error message itself we can get that this error can be resolved easily by specifying the name space for which you want to prioritize the search when URL request is made to the server. Lets see how:
  1. routes.MapRoute("DemoRoute""{controller}/{action}/{id}",  
  2.     new {controller = "Home", action = "Index", id = UrlParameter.Optional },  
  3.       new []{"URLsAndRoutes.AddonControllers"});  
Here, specifying the new array of strings and saying the MVC routing to search for the controller everytime in the AddonControllers or the specified namespace first and if not found then move into the controllers folder to search for the match of the URL request. This really would of high use when the requirement is as such.

Conclusion & Points of Interests

Thus here we discussed about one of the critical concepts of MVC. Routing and pipelining is a mandatory concept to know to start with learning MVC. Using MVC application and not using Routing is not wise. There are opportunities to manipulate and customize the Routing system, so it is always advisable to use Routing whenever MVC application is in picture. The URL segment variables Controllers, Actions and parameters are required to customize the URLs to the convenience of the end users. The routing can narrow down the search of the routes if the maximum hit URL requests are ordered highly in the Route Collection.

References

Asp.NET MVC Professional by Adam Freeman.

Up Next
    Ebook Download
    View all
    Learn
    View all