Thursday, February 19, 2015

Routing in MVC5

In old web frameworks like classic ASP, there used to be a direct relationship between the URL requested and the file on disk. For example, when we requested, we used to get the index.aspx file. In contrast, in ASP.NET MVC, URL is generally mapped to an action method in controller class which decides what is supposed to be the response.


Routing is the mechanism for URL pattern matching to controller action method. Simplistically, it maintains a list of patterns in a table and when a request comes in, it matches those patterns one by one and returns the first successful match. If none of the patterns match, we get 404 Not Found error.

We can add new routes by adding to the static routes property of RouteTable class. Let's take a look at
the default code we find in RouteConfig.cs file.

public static void RegisterRoutes(RouteCollection routes)




                name: "Default",

                url: "{controller}/{action}/{id}",

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



In the code above, routes.MapRoute is setting the name of the route to Default. The name is not going to affect the URL mapping, it's just there to uniquely identify the route. The url specifies the pattern to be matched. So, here it says that controller name, action name and id are supposed to be separated by forward slash "/". Then defaults as the names suggests assigns defaults as HomeController and Index action method. The id does not have a default value but it is specified as optional.
So when I run the application for the first time without any controller and action name in URL, I get the response provided by Home Controller's Index Action method.

routes.IgnoreRoute is basically an instruction to routing mechanism to ignore any request that matches this pattern. So the statement will ignore all the requests which have the extension .axd. On such requests normal ASP.NET handling will occur which is dependent on existing HttpHandler mappings.

Considerations for Routing

When routing handles URL requests, it tries to match the URL of the request to a route. Matching a URL request to a route depends on all the following conditions:
  • The route patterns that we have defined or the default route patterns, if any, that are included in our project type.
  • The order in which we added routes to the Routes collection.
  • Default values that we have provided for a route, if any.
  • Constraints that we have provided for a route, if any.
  • Whether we have defined routing to handle requests that match a physical file.

Route Debugger

Debugging routes can be frustrating some times. We might think that this pattern should match but somehow we don't get the expected result. For this purpose, I will highly recommend RouteDebugger. Simply add the Nuget package called RouteDebugger. Once added, we can control the visibility of the table using this entry in web.config file.

<add key="RouteDebugger:Enabled" value="true" />



Routing is pretty easy to start with but it takes a long time to master. Routes are also part of the application interface to the user and having proper intuitive routes improve user experience as well as search engine optimization.
I will cover Attribute Routing in my next post. For future updates to my weekly blog, please subscribe to the blog and follow me on Twitter. Until then Happy Coding :)

No comments:

Post a Comment