In my previous post I promised to start with exploration of
MvcHanlder as entry point of ASP.NET MVC application.
MvcHandler plays the major role in MVC infrastructure, but as I fugured out - the whole story does not begin there. The story is actually begins inside ASP.NET framework.
ASP.NET framework is heart of ASP.NET MVC application. Basically, ASP.NET is a request processing engine. It takes incoming request as input and sends it through internal pipeline till the end point. The architecture allows extensibility, you could plug either into pipeline (modules) or end points (handlers). Interesting fact that ASP.NET designed to be decoupled from actual request source. So, the source of request could be any application - IIS, Cassini or any custom one.
We could imagine that each request pass through the set of filters (internal pipeline) lands up on request handler (end point), request handler creates a response object and sent it back. There are a lot of hidden details of dealing with managed/unmanaged code that you could find in this brilliant article by Rick Strahl.
For us it is important to understand two basic abstractions, HttpModules and HttpHandlers.
From the developers point of view the difference between those are: one implements
IHttpModule interface another implements
Module participates in the request processing of every request in order to change or add to it in some way.
Handler is responsible for handling the request and producing the response for specific content types.
ASP.NET MVC is HttpHandler.
There a bunch of default HttpModules and HttpHandlers in your IIS configuration. Just take a look here - %WINDOWS%\Microsoft.NET\Framework\v4.0.30319\Config\web.config.
But if you look closer you won’t notice any mention of
System.Web.Mvc.MvcHanlder. Hey. what’s wrong? How the runtime actually knows that
MvcHanler have to be called to handle our request? That was absolutely unclear to me at very beginning.
I opened WebRuntime.sln and found all references to
MvcHandler. It is only one place there, in the
MvcRouteHandler (MvcRouteHandler.cs) class. MvcRouteHandler implements
IRouteHandler interface, with one single method
GetHttpHandler. I put the breakpoint there and started application for debug.
As application got started I halted on breakpoint with such call stack:
Let’s try to read and decrypt it.
It begins with
WebDev.WebHost40.dll (Cassini Web Server) that receives request and
System.Web.HttpRuntime.ProcessRequest. Here is ASP.NET framework start to work:
HttpApplicationinstance, initialize it and calls either
global.asax.csof our application it is clear that
System.Web.HttpApplicationimplementation is inherited and
System.Web.HttpApplicationis a usual
IHttpAsyncHandler. So, async
BeginProcessRequestthere is a cycle that iterates all modules and execute each of it. Reflector code looks messy, but the point is clear
UrlRouting is an HttpModule, registered in global web.config:
The goal of this module is basically matching incoming request by URL with pre-defined Route configuration and return corresponding handler for this request. In
PostResolveRequestCache method it gets the RouteHandler and asks for corresponding HttpHanlder.
Wow, I didn’t know that!.. UrlRouting plays a major role inside the MVC infrastructure. But there the Routes are initialized and how
MvcRouteHandler is associated with request?
I should actually start from here,
Application_Start() is entry point of application. By my journey turn to be just other way around.
RegisterRoutes we register route by giving: route name and constraints.
MapRoute is actually extension method in RouteCollectionExtensions.cs.
Now, you can see how the route with particular URL is being register and associated with
Now it is time to get deep breath and finally understand the route of HTTP request to MvcHttpHandler. Here we go:
ProcessRequestmethod, but interesting stuff going in
GetApplicationInstancemethod that get application instance and calls it
Application_Init()registers routes that map URL (like “home/index”) with
UrlRoutingmodule matches incoming request URL with registered collections of Routes
MvcHandleris being returned by GetHttpHandler of MvcRouteHandler
That’s the routing that is being executed each time you put URL and hit your browser button.
I hope next time we indeed look closer to