Reputation: 5683
I'm trying to understand and learn the architecture and pipeline of ASP.NET. So far I understand the architectural overview:
MvcHandler
MvcRouteHandler
Now what I don't understand (or can't find any resource on the web documentating this part), is how does the asp.net runtime detect which HttpHandler it has to select for it's request? So how does it know based on my Visual Studio solution that it's an MVC application for example? How does it figure it out that it should use the MvcHttpHandlers? Is there an assembly scan with Reflection somewhere in the HTTP pipeline? Because it certainly isn't a configuration telling the runtime to use the MvcHandler, or is it?
So basically at what exact point is the HttpContext.CurrentHandler
being set?
Upvotes: 1
Views: 602
Reputation: 2128
Application_Start
When the request arrives to IIS and the endpoint corresponds to an Asp.Net application, then the first event raised is the
Application_Start
in theSystem.Web.HttpApplication
object.
RouteTable.Routes.Add
Into this event of an Mvc app you can set the routing rules that do match endpoint urls with Controllers and Actions methods in the application and the relative
IRouteHandler
object type, that will betypeof(MvcRouteHandler)
.
(see Scott Guthrie post)
HttpApplication.MapRequestHandler
Therefore, soon after that, when the routing table has been setted up, in the subsequents events (or better in the methods that compose the pipeline orchestrated by the Asp.Net Framework under IIS control (integrated pipeline)) of the Asp.Net http request management, when it needs to know how to manage the http request itself (
HttpApplication.MapRequestHandler
), it get parsed the url in theHttpContext
object against the rules in the routing table, and when it get found a matching, it is instatiated the right type of its handler,MvcRouteHandler
in our case, which will return theIHttpHandler
object by the methodGetHttpHandler(RequestContext)
:MvcHandler
.
(see Msdn MvcRoutHandler.GetHttpHandler)
MvcHandler.ProcessRequest
MvcHandler in turn, will give start to the real MVC request handling through the Asp.Net pipeline event
ProcessRequest
: and so will be instatiated the right Controller through theControllerFactory
, and will be called theExecute
method of theController
abstract base class of the instatiated controller object, and finally the right Action through theActionInvoker
.
(see Msdn MvcHandler.ProcessRequest, and The Asp.Net Mvc pipeline)
final note on RouteCollection.MapRoute
The Visual Studio starter template for MVC projects creates project that useMapRoute
extension method instead ofRouteTable.Routes.Add
. This method is very useful because avoids to have to use alwaystypeof(MvcRouteHandler)
expression when adding new url routing to the project.
(see Asp.Net Routing and Asp.Net Mvc)
Upvotes: 1