Please enable Javascript to correctly display the contents on Dot Net Tricks! MVC Request Life Cycle

  Author : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 189,586   
Support : MVC5, MVC4 & MVC3

While programming with MVC, you should be aware of the life of an MVC request from birth to death. In this article, I am going to expose the MVC Request Life cycle. There are seven main steps that happen when you make a request to an MVC web applications. For more details refer Detailed ASP.NET MVC Pipeline

  1. Routing Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding IRouteHandler for that request. The default one calls the MvcHandler. The routing engine returns a 404 HTTP status code against that request if the patterns is not found in the Route Table.

    When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system what to do with any requests that match these patterns. An application has only one Route Table and this is setup in the Global.asax file of the application.

     public static void RegisterRoutes(RouteCollection routes)
     routes.MapRoute( "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

  2. MvcHandler

    The MvcHandler is responsible for initiating the real processing inside ASP.NET MVC. MVC handler implements IHttpHandler interface and further process the request by using ProcessRequest method as shown below:

    protected internal virtual void ProcessRequest(HttpContextBase httpContext)
     SecurityUtil.ProcessInApplicationTrust(delegate {
     IController controller;
     IControllerFactory factory;
     this.ProcessRequestInit(httpContext, out controller, out factory);
  3. Controller

    As shown in above code, MvcHandler uses the IControllerFactory instance and tries to get a IController instance. If successful, the Execute method is called. The IControllerFactory could be the default controller factory or a custom factory initialized at the Application_Start event, as shown below:

    protected void Application_Start()
     ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
  4. Action Execution

    Once the controller has been instantiated, Controller's ActionInvoker determines which specific action to invoke on the controller. Action to be execute is chosen based on attributes ActionNameSelectorAttribute (by default method which have the same name as the action is chosen) and ActionMethodSelectorAttribute(If more than one method found, the correct one is chosen with the help of this attribute).

  5. View Result

    The action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The result type can be ViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.

  6. View Engine

    The first step in the execution of the View Result involves the selection of the appropriate View Engine to render the View Result. It is handled by IViewEngine interface of the view engine. By default Asp.Net MVC uses WebForm and Razor view engines. You can also register your own custom view engine to your Asp.Net MVC application as shown below:

    protected void Application_Start() 
     //Remove All View Engine including Webform and Razor
     //Register Your Custom View Engine
     ViewEngines.Engines.Add(new CustomViewEngine());
     //Other code is removed for clarity
  7. View

    Action method may returns a text string,a binary file or a Json formatted data. The most important Action Result is the ViewResult, which renders and returns an HTML page to the browser by using the current view engine.

What do you think?

I hope you will enjoy the Asp.Net MVC request life cycle while programming with Asp.Net MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Free Interview Books
26 OCT
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM-09:00 AM IST

Know More
24 OCT
MEAN Stack Development (online)

MON-FRI 06:00 AM -07:30 AM IST

Know More
24 OCT
AngularJS Development (online)

MON-FRI 08:00 PM-09:30 PM IST.

Know More
23 OCT
PPC Marketing (offline)

Sat, Sun 09:00 AM-10:30 AM IST

Know More
22 OCT
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

20 OCT
NodeJS Development (online)

MON-FRI     06:00 AM - 07:30 AM IST

12 OCT
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     09:30 PM-11:00 PM IST

17 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     05:00 PM-06:30 PM IST

NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST


Professional Speaks