Wednesday, January 23, 2013

ASP.NET MVC Pipeline

Download diagram: PDF, JPEG


0. App initialisation

·         When the application starts up, like any ASP.NET application, it runs Global.asax's Application_Start () method.

·         Route objects are added to the static RouteTable.Routes collection (which is of type RouteCollection). These will be inspected later when each request is received.

·         Each Route object defines a URL pattern to be matched and the controller to be used in this case. Optionally, you can specify a controller action and a custom IRouteHandler if you don't want to use the default (which is MvcRouteHandler).

·         If you're implementing a custom IControllerFactory (for example, Castle Windsor), you can set this as the active controller factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property.


1. Routing

·         Overview: Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern. MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table.


·         The combination of System.Web.Mvc.UrlRoutingModule  and System.Web.Mvc.MvcHandler references in the web.config give responsibility for handling all incoming requests to MvcHandler.

·         First, MvcHandler calls Routes.GetRouteData() which matches the incoming request against the list of Route objects added in Application_Start(). The appropriate Route is chosen and a RouteData object prepared. This references the appropriate IrouteHandler (via RouteData.RouteHandler) and IController to be used.

·         Next, the IRouteHandler's GetHttpHandler() is called, returning an IHttpHandler, whose ProcessRequest() method is finally invoked. The default IHttpHandler as returned by MvcRouteHandler is, again, MvcHandler, which performs steps 2-4 below.



2. Instantiate and execute controller

·         Overview: The active IControllerFactory supplies an IController instance

·         MvcHandler's ProcessRequest() method calls ControllerFactory.Instance.CreateController(), passing context information including the type of controller obtained in the RouteData object previously.

·         The active IControllerFactory is responsible for instantiating and returning an appropriate IController. Usually, this will be a subclass of the Controller base class.

·         The IController's Execute() method is then called. If this is a subclass of Controller, steps 3 and 4 below are performed.


3. Locate and Invoke Controller Action

·         Overview: The controller invokes its relevant action method, which after further processing, calls RenderView()

·         The Controller.Execute() method uses the RouteData and other context information to pick the appropriate action method. This method must have a [ControllerAction] attribute to be eligible for selection. It also maps incoming request parameters (querystring, form etc from the IHttpRequest context object) to the parameter list of the action method.

·         Most controllers inherit from the Controller base class. For controllers that do so, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and then calls that method.

·         The controller calls its own InvokeAction() method, passing details of the chosen action method, which, predictably, invokes the action method. This is where your code finally runs.

·         Within your [ControllerAction] method, you're expected to call the Controller's RenderView() method. By this time, you will have populated the controller's ViewData property. RenderView() performs step 4 below.


4. Instantiate and render view

·         Overview: The IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.

·         The view subsystem follows the same factory pattern as routing. That is, the Controller object has a property called ViewFactory which is of type IViewFactory. The IViewFactory interface defines a method called CreateView(), which takes a view name and other context information, using which it instantiates and returns an IView.

·         The controller can now invoke the IView's RenderView() method, supplying the necessary context information, which includes ViewData and the IHttpResponse object to which it can push any text or binary data into the response. This response data may be HTML, an image, or any other binary or text data.


Summary / Conclusion

As a developer working with ASP.NET, you won't need to worry about the intricacies of IRouteHandler and IViewFactory and the like very often. The typical development process is simply:


  • From Global.asax, add a Route object representing a certain URL pattern you want to catch and map to a controller / action
  • Add a Controller subclass, whose [ControllerActions] should be invoked in response to requests, populating ViewData
  • Add a view template that uses ViewData to render some HTML


The thing to remember is that there are many extensibility points all over the framework, so if you want to build a custom view engine or a custom request routing system, you can hook in at the appropriate points. Hopefully the diagram and explanation above will prove useful if you're just starting to do that.


Reference: Link


Thanks & Regards,



Arun Manglick
Project Manager (PMP, CSM, MS-Project)

Digital Group InfoTech Pvt. Ltd.

Pyramid building, Plot No.5

Rajeev Gandhi InfoTech Park, Phase I,

Hinjewadi, Pune - 411057,  India  

Office: +91 20 66532084

Fax: +91 20 66532052

Mobile: +91 9850901262












No comments:

Post a Comment