ASP.NET detection of HttpHandler MVC vs. WebForms

QuantumHive

I'm trying to understand and learn the architecture and pipeline of ASP.NET. So far I understand the architectural overview:

  • on how we get from client to the IIS webserver (request)
  • via ISAPI extension to the ASP.NET runtime
  • from there on into the HTTP Pipeline
  • and ASP.NET calling the HttpModules and HttpHandler
  • in case of using MVC, selecting the MvcHandler
  • which is returned by the MvcRouteHandler
  • etc etc

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?

Ciro Corvino

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 the System.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 be typeof(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 the HttpContext 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 the IHttpHandler object by the method GetHttpHandler(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 the ControllerFactory, and will be called the Execute method of the Controller abstract base class of the instatiated controller object, and finally the right Action through the ActionInvoker.
(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 use MapRoute extension method instead of RouteTable.Routes.Add. This method is very useful because avoids to have to use always typeof(MvcRouteHandler) expression when adding new url routing to the project.
(see Asp.Net Routing and Asp.Net Mvc)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related