Understanding Routing In MVC

This article describes how routing is working in MVC.

Before writing anything about MVC we must understand what MVC is and what Routing is in MVC. This article describe how routing is working in MVC.

MVC stands for Modal View Controller.

Modal View Controller

As per my understanding the fact behind the MVC is shown in above image.

MVC Routing Engine:

MVC Routing Engine

Routing

Routing is a very complex topic in MVC. So more we make our hands dirty on routing more we can play easily in MVC. Routing is responsible to map the incoming request with the appropriate controller. And to call that routing is the start point in MVC.

Look at the image below:



Its showing routing data gets filled up in App_start event that exists in Global.asax file. The important method is “RegisterRoutes”.

  1. public static void RegisterRoutes(RouteCollection routes)  
  2. {  
  3.     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.     routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new  
  5.     {  
  6.         controller = "Home", action = "Index", id = UrlParameter.Optional  
  7.     });  
  8. }  
The above method is responsible to map the route. Let’s get into the details of this method line by line.
  1. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
This represents clear instruction for the incoming request, Please ignore anything which ends with .axd and having any other additional parameter.

 

  1. routes.MapRoute(  
  2.    name: "Default",  
  3.    url: "{controller}/{action}/{id}",  
  4.    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  5. );  
This actually defines the default route. Any incoming request first hit the Controller then method with defined parameter ("{controller}/{action}/{id}",). In the next line it is defined the default constructor is “Home” and method to be called is “Index”. The “id” parameter is optional as we can have method without any parameter.

How to customize Routes

Now in case user want to access ”IAmThereController” (as defined in code), then our default route will work for the same.

Let us see how

I have defined new controller, here's the code:
  1. public class IAMThereController: Controller  
  2. {  
  3.     //  
  4.     // GET: /IAMThere/  
  5.     public string Index()  
  6.     {  
  7.         return "This is default Method";  
  8.     }  
  9.     public ActionResult Welcome(string name)  
  10.     {  
  11.         ViewBag.a = "welcome " + name;  
  12.         return View();  
  13.     }  
  14. }  

 

  1. Request: http://localhost:61833/Iamthere

    Output:

    Output

  2. The second request is very interesting,

    Request: http://localhost:61833/iamthere/welcome/nishant

    Output: It will not print “Welcome Nishant”, check below. I have debugged the same and found name is coming as null. Look at below:

    Code

    And it will not print “Welcome Nishant

    Welcome Nishant

To fix this error: We need to define a new Route that tells the routing handler how to navigate to an action method with name parameter. I have changed my route:

  1. routes.MapRoute(  
  2.    name: "Default",  
  3.    url: "{controller}/{action}/{name}",  
  4.    defaults: new  
  5.    {   
  6.       controller = "Home",   
  7.       action = "Index",   
  8.       name= UrlParameter.Optional 
  9.    }  
  10. );  
String

run

Hope this will help to understand the basic concept of Routing.