Geeks With Blogs

Sankarsan Bose .NET Diaries

While requesting some resource from an web server we normally type an URL like  in the browser address bar.In this case the URL maps directly to a file residing in the server.Now say we have an URL like .In this case the web application has to be intelligent enough to parse the URL and find out which resource or request handler it will map to.This technique of parsing URL patterns and deciding on the requested resource is known as URL Routing.

Along with ASP.NET MVC framework we have a framework for URL routing as well.The functionality of routing is provided by classes in the assembly System.Web.Routing.dll.In this post I will discuss about the important classes and interfaces of the routing framework and steps in the process of creating an instance of the right controller from the URL pattern.

We have the System.Web.Routing.IRouteHandler interface (which inherits from IHttpHandler interface) defining the method System.Web.IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext).The default route handler provided by the ASP.NET MVC framework which implements this interface is System.Web.Mvc.MVCRouteHandler.This class instantiates the System.Web.Mvc.MVCHandler which in turn creates the controller instance via controller factory. The key entity class is System.Web.Routing.Route which inherits from System.Web.Routing.RouteBase class.This class is an abstraction of URL route.It stores the url pattern string,routing constraints and the reference to the route handler.There is another class System.Web.Routing.RouteData which stores the values obtained after parsing the url at runtime.This entire routing system is plugged into the ASP.NET pipeline via the System.Web.Routing.UrlRoutingModule which implements IHttpModule.

The basic steps in the routing process are as follows:

  • System.Web.Routing.RouteTable maintains a static reference to collection of Route objects.This is initialised in the Application_Start event.That's why in an ASP.NET MVC application project we can see the following lines of code by default added in global.asax.cs Application_Start event.


         public static void RegisterRoutes(RouteCollection routes)

                  "Default",                                              // Route name
                  "{controller}/{action}/{id}",                           // URL with parameters
                 new { controller = "Home", action = "Index", id = "" }  // Parameter defaults


  • The UrlRouting module intercepts request.This is the also added by default in web.config of ASP.NET MVC application project as shown below:

         <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,Version= ,Culture=neutral,   PublicKeyToken=31BF3856AD364E35"/>

  • The UrlRouting module
    • Creates the RouteData object by parsing the url.
    • The System.Web.Routing.RequestContext is created.This object maintains reference to the HttpContext object ,RouteData and Route.
    • Invokes the GetHttpHandler method of the associated MVCRouteHandler instance and passes the RequestContext object as parameter.
  • MVCRouteHandler creates an instance of MVCHandler.
  • MVCHandler
    • Invokes the CreateController method of System.Web.Mvc.DefaultControllerFactory.This method creates the instance of System.Web.Mvc.IController type.
    • Invokes the Execute method of IController instance.
    • Invokes the ReleaseController method of System.Web.Mvc.DefaultControllerFactory.This method disposes the instance of the controller.

This in brief what happens as part of the URL routing process.In my next post I will discuss about developing a custom route handler.

Posted on Saturday, January 10, 2009 12:55 AM ASP.NET MVC | Back to top

Comments on this post: ASP.NET MVC Routing

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © sankarsan | Powered by: