[ASP.NET MVC] Execution Process

OK, everybody talks about MVC and you know what it stands for.

  • M (Model): Data objects – business logic, validation, and DB access
  • V (View): UI
  • C (Controller): well, it does many things, handles user interaction, works with models, and selects a view, etc.

You know (or others say) MVC is good. But in this post, let’s put the philosophy aside and look at how MVC works when you enter an address in your web browser. 

 

1. Prerequisites: HttpHandler & HttpModule

Before we talk about the execution process, let’s review what HttpHandlers and HttpModules are.

ASP.Net handles each request in a pipeline and HttpHandler and HttpModule are the two different processing points in the pipeline.

  • HttpHandler: HttpHandler actually processes each request. It is a class that implements the “IHttpHandler” interface.
  • HttpModule: HttpModule is like a filter; intercepts each request and does its task if necessary.

 

2. Overview of MVC Application Execution Process

An ASP.NET MVC application is not that much different from your old ASP.NET web form application in terms of the execution process.

The most noticeable feature of MVC is the URL, right? The URL does not point to the physical file. So before processing the request, MVC should figure out how to handle the URL. That’s the job of the “UrlRoutingModule object, which is an HttpModule.

The “UrlRoutingModule” interprets the route information of the request and finds out which route should be used. And then “UrlRoutingModule” retrieves the Route handler object and finally the MVC handler object.

The MVC handler object creates a controller object and executes the controller, which will invoke an appropriate action. A typical action method manipulates model data and executes the result, which creates the output (view).

 

3. MVC Application Execution Process Steps

  1. stage 0 (Only for the first request):  Route objects (in the Global.asax) are added to the “RouteTable” collection.
  2. stage 1 (Perform routing): The “UrlRoutingModule module intercepts the request and tries to match each “Route” object in the “RouteTable” collection. When it finds the first matching “Route” object, the “UrlRoutingModule” creates the “RouteData” object and the “RequestContext (IHttpContext)” object.
  3. stage 2 (Create a Route Handler): From the selected “Route” object, the “UrlRoutingModule” object obtains the “IRouteHandler object that is associated with the “Route” object. Typically, in an MVC application, this will be an instance of “MvcRouteHandler.
  4. stage 3 (Create a MVC Request Handler): The “MvcRouteHandler” object creates an instance of the “MvcHandler class. And the “RequestContext” object is passed to the handler.
  5. stage 4 (Create a controller): The “MvcHandler” object uses the “RequestContext” instance to identify the “IControllerFactory object (typically an instance of the DefaultControllerFactory class) to create the controller instance.
  6. stage 5 (Execute the controller): The “MvcHandler” object calls the controller’s “Execute method.
  7. stage 6 (Invoke an action): From the route information, the action method to be called is determined and the action method is invoked.
  8. stage 7 (Execute the result): A typical action method executes a result (most commonly the ‘ViewResult’) and returns the result.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s