MVC Routes and Controller- Part 1

Lets start with simple but important question,

How does knows how to deliver a request to a specific controller. Like /home/about to the Home Controller? The answer of the question is routing engine.

The routing engine is a core part of ASP.NET. Its not tide to the MVC framework. You can use the route request for Web Forms , WCF services , WebAPI etc. But in MVC we use the route engine to directly request to our controller.


Here the first line “Default” is the friendly route name .

The second line “{controller}/{action}/{id}”  is URL with parameters or we can say this is a pattern for the route.

And the third or last line “new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }”  represents the default parameters for the route.

Think about the goal of the routing engine. Its job is to examine the url and figure out where to send it for processing.


In MVC we want to specify the URL. Here  Home points to {controller} and Contact points to {action}. So it will look into Home Controller and inside that controller it will invoke the contact action and perhaps other data. Following is the code inside home controller:


So the words inside the curly braces are the URL parameter names. If the routing engine doesn’t find the specific data in the URL , like the controller name or the action name it can use one of the default values that we specify in the parameter defaults.

defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }

Let us introduce the ASP.NET magical file “global.asax”. If I open it up, it will look like the following:


You can see we have a class here derived from “HttpApplication” and this allow us to hook into some application events like “Application_Start”.  This method will be magically invoked by ASP.NET before you process your first http request. So when your application first running the code here will execute one time before any of your controller start executing and so there is where we put some configurations that the application needs like the routing configuration.

Lets make a summary, when an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table. The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.
Imagine that you enter the following URL into your web browser’s address bar:

The Default route maps this URL to the following parameters:
controller = Home
action = Contact
id = 3

When you request the URL /Home/Contact/3, the following code is executed:

The Default route includes defaults for all three parameters. If you don’t supply a controller, then the controller parameter defaults to the value Home. If you don’t supply an action, the action parameter defaults to the value Index. Finally, if you don’t supply an id, the id parameter defaults to an empty string.

That is all for today. Stay tuned for the next part…. Hope you enjoyed 🙂

4 thoughts on “MVC Routes and Controller- Part 1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s