Tuesday, August 14, 2012

mvc3 routes tutorial ( An introduction to MVC routes)

Routing overview

When a person types in something like  into his browser window, what is shown above is a summary of what happens. The request goes to the webserver, where the application sends the request to an action method in a controller. The action method does some logic and passes some data into a view. That view uses that data and creates an HTML output which is returned to the user.   Now the question naturally arises, how does the application decide which action method to send the request to?  That is where routing comes in. The routing code in the global.asax.cs page of your application is where you decide which controller/action to send the request to. 

When you create an mvc application by default this line of code gets added in the Global.asax.cs of your application

                new { controller = "Home", action = "Index", id = UrlParameter.Optional }


If you remove that line of code and have no other routing added, then all calls to your application would result in a 404 error. Lets dissect the above line of code and see what the parameters to the Maproute method mean.

The route name
This is just a name for your route. You can call the route anything. If you want you can also call it hulk :)

The route mapping
What this means is that the first segment after the url, will map to the controller name, the second segment maps to the action name and the third segment to a parameter named id. A segment is demarcated by the forward slash   (/segment1/segment2)

If we try to access a url of the form http://localhost:51993/Home/Index/2.

The action method that accepts it, can access the parameter id, if the action method has the signature

public ActionResult Index(int? id)

Three things to note here. 
  • The action method accepts the parameters in the order they are in the url.
  • The action method parameter has to have the same name as defined in the mapping
  • Since the parameter is optional (explained below), the int in the action method has to be nullable. 
  • {} is a placeholder.  (More on this in points to note section below)

The Default values
new { controller = "Home", action = "Index", id = UrlParameter.Optional }

Here we are setting default values for each segment. If the route segments are missing, it will assign these values to those segments. For instance, If I type in the url
 Since the segment for action is missing, it will get set to index. We also say id=UrlParameter.Optional which means its ok to skip the id parameter. So due to the default values, the applications ends up looking for

Some points to note

  • If you try to reach a url that has more or less segments than what is defined in the mapping, you will get a 404 error.
  • If you have multiple route mappings defined, then the application will pick up the very first one that matches. Hence always define routes from most specific to most general. 
  • Suppose I change the default routing to 
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 

Note here that instead of  {controller} (inside the placeholder{} )I have hard coded Home. 
If this is the case, a request to the url http://localhost:51993/Account will throw a 404 error.

The reason being, the application looks for urls of the form 

If the application does get a request of the above form, it will map the parts after that to the action and id respectively.

  • Generally you need to define you own route mappings only if your action method has more than one parameter, or it accepts parameters of the non native types (an instance of a class for example).
Route Debugging

Use this tool for route debugging

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator