MVC Request Life Cycle




ASP.NET MVC Request Life Cycle

Before going to understand the MVC request life cycle we will have a small introduction to ASP.NET MVC.

ASP.NET MVC is a new web application framework from Microsoft, it is a wrapper around ASP.NET and MVC is not a replacement for ASP.NET its a new architecture which is evolved from 3 layered architecture to develop web applications which is high in performance and scalability and testability, MVC built on top of ASP.NET and it can utilize the features of ASP.NET authentication/authorization and session variables. MVC stands for model, view & controller where model represents data, view represents UI and controller acts like code behind.MVC has more advantages over ASP.net web forms. In MVC there are is no concept of pages and controls, post backs or view state, complicated ASP.NET event life cycle.
Basically, MVC is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers. Hence in Asp.net MVC we play with controllers, actions, and views.

Little  more about model, view, controller .

Model: It retrieve the application data from Database and it also contain business logic to change the state mention by controller
View: Components that display the applications user interface (UI)
Controller: Components that handle user interaction and display the UI with help of Model data

Advantage of MVC:

  • Light weight framework
  • Enables Test Driven Development
  • URL based routing
  • No ViewState and PostBack events
  • Loosely Coupled application development
  • Provides clean separation of concerns(SoC)
  • Integrated with existing features such as Master Pages, Security, and Authentication
  • follows agile methodology
  • no event driven programming


While developing web application using ASP.Net MVC, we should understand how ASP.NET MVC processes our requests and how many main stages there are in this process.

There are mainly seven stages in the ASP.Net Request Life Cycle.



Life cycle of MVC request is a series of steps involved in processing client request. Regardless of technology and platforms almost all the web frameworks have one or other type of Request life cycle and MVC is no different. Understanding the life cycle of any web framework helps better leverage the features of request cycle for processing requests.
In this article I am going to explain what exactly happens in ASP.NET MVC request life cycle and what each step in the life cycle does and how we can leverage it further based on our requirements. This article specifically targets Page life cycle which is different from Application life cycle. A typical Application life cycle contains Application start and Application End events; however http Life cycle is something which is repeated for every request. Since application events are also part of life cycle, we will see them as we move along.

MVC request life cycle:


       1)   The entry point for MVC Request life cycle is URLRoutingModule, the incoming request from IIS component is handed over to URL Routing module which intercepts the request and looks into Routing table to figure out which controller the incoming request maps to.

      2)  Routing Table is a static container of routes defined in MVC application with corresponding controller action mapping. If the route is found in the routing table MVCRouteHandler executes and brings the instance of MVCHttpHandler. Together they act as a gateway into the MVC Framework.

   3) MVC handler (MVCHttpHandler) begins initializing and executing controller. The MVCHttpHandler also takes of converting route data into concrete controller that is capable of serving the request.

     4)  MVC handler does all this with the help of MVC Controller factory and activator which are responsible for creating an instance of the controller. This is also the place where the Dependency Injection is performed if the application has been designed to invoke parameterized controller constructor and satisfy its dependencies.

     5)    After the controller instance is created the next major step is to find and execute the corresponding action.

    6)      A component called ActionInvoker finds and executes the action defined in routing table. Before the action method is called model bindings takes place which maps data from http request to action method parameters.

    7)      After the model binding, action filters are invoked which includes OnActionExecuting filter. This is followed by action execution and Action Executed filter execution and finally preparing Action Result.

    8)   Once the Action method has been finished executing the next step is Result execution.

    9)      MVC separates the action declaration from Result execution. If the Result from action execution is view, then depending upon configuration, ASPX or Razor view engine will be called to find and render the html view as a response of http request. If the result was not view then it’s passed as-is to http response.

Below image shows the different operations involved in MVC request life cycle at high level view.








Comments

Popular posts from this blog

Download Excel File using AJAX in JavaScript

How to Capture Image using web camera in Asp.net and C#.NET / VB.NET

How to Install GIT ?