Today, I will try to explain ASP.NET MVC Routing from prespective of a ASP.net Webforms developer. Attempt over here is to stay away from page lifecycle and be focused till the instantiation of the page/controller class.

ASP.NET Web Forms

As such there is no routing in ASP.NET Webforms. But if you look at a page class, often you would have a question how is the page class instantiated. Class responsible for creating a page object is PageHandlerFactory. This class is registerd in web.config and in simple words the request pipeline is like.

  1. Execute modules registered in web.config
  2. Get the Page object using the PageHandlerFactory regiestered in web.confg. PageHandlerFactory’s GetHandler returns an Object that implements IHttpHandler, in case of .aspx request it is Page class that implements the IHttpHandler interface. But the actual instance returned is not the codebehind class but a class derived from code behind class (Details)
  3. Invoke the ProcessRequest method of the Handler returned by the PageHandlerFactory. After this step the page events are invoked.

ASP.NET MVC

Now, what happens in case of MVC is that Page (which is an IHttpHandler ) is replaced by another class that implements IHttpHandler that is MvcHandler, and the class responsible for creation of MvcHandler is MvcRouteHandler (method GetHttpHandler) which is equivalent of PageHandlerFactory (GetHandler).

But the question is what stage is it determined to use MVC objects but not the Webform ones. This is done by a module called UrlRoutingModule, UrlRoutingModule attaches itself to PostResolveRequestCache and PostMapRequestHandler events of the HttpApplication instance and overrides the default behaviour of MapRequestHandler stage.

At this stage the Route Table to which routes are set in Application Startup (as following) is used to determine if the URL match to one of the routes defined, by default it uses MvcRouteHandler, which can be overwritten by custom routehandler.

public class MvcApplication: System.Web.HttpApplication {
 public static void RegisterRoutes(RouteCollection routes) {
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new {
    controller = "Home", action = "Index", id = ""
   } // Parameter defaults
  );

 }

 protected void Application_Start() {
  RegisterRoutes(RouteTable.Routes);
 }
}

So, in simple words UrlRoutingModule returns MvcRouteHandler which returns MvcHandler, now MvcHandler is the class that create an instance of the controller using controller factory.

Refs

http://blog.thekfactor.info/posts/tag/mvchandler/

http://blogs.charteris.com/blogs/gopalk/archive/2009/01/20/how-does-asp-net-mvc-work.aspx

http://msdn.microsoft.com/en-us/magazine/dd347546.aspx

http://www.4guysfromrolla.com/articles/051309-1.aspx

http://www.4guysfromrolla.com/articles/050510-1.aspx

http://www.beletsky.net/2011/06/inside-aspnet-mvc-route-to-mvchanlder.html

http://www.dotnetframework.org/default.aspx/4@0/4@0/untmp/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/xsp/System/Web/Routing/UrlRoutingModule@cs/1305376/UrlRoutingModule@cs