Understanding ASP.NET MVC Application Life Cycle

By | April 21, 2015

How ASP.NET MVC application works when the moment we type in the URL in browser and shoot a request? Let’s discuss the Life cycle of ASP.NET MVC application.

A developer must understand the life cycle of ASP.NET MVC so that he gain a full control over the developer platform. First let us see briefly, how the traditional ASP.NET application works. When the user sends a request to IIS, it validates the requested file extension & creates an application domain where the app can run. Later HttpApplication object is created once after all the HTTP object such as HttpContext, HttpRequest and HttpResponse are created. Then the request is processed by the two prominent pillars called HttpModule and HttpHandlers.

Hope that would be a brushup on legacy technique. Now let’s see the Life Cycle of MVC (Model View Controller) in detail.

ASP.NET MVC Life Cycle in Eagle’s view

When the user sends from the browser, it’s been handled by Routing concept and which navigates it to the appropriate Controller where controller picks up the relevant View and send it as a response to the User. The below diagram shows you the high level view of how the ASP.NET MVC processes a request.ASP.NET MVC Application Life Cycle

The first point should be noted in ASP.NET WebForms and ASP.NET MVC difference is that in MVC we don’t have the Page concept (In order to understand the difference between WebForms and MVC in details, follow here), whereas in Legacy we do have pages like .aspx, ashx etc. So if your question is, Then how does the requests are been pointed to in MVC? The answer, Is via Controllers. This special class is responsible for generating the response and sending the data back to the client.
For instance, the URL

http://www.webdevelopmenthelp.net/Book/Create

Says that it is requesting for the Controller class name “Book” and the Action Method name called “Create”.

Note: A Controller class can contain numerous Action Methods, if it needs to be accessed then it should be a public method.

ASP.NET MVC File Structure

Global.asax file is responsible for handling the Application and Session level events involved in ASP.NET MVC life cycle. IIS runs this as the first file when the request comes in. Let us look at the project and how the files been structured.

Global.asax in ASP.NET MVC
As you can see in the above image that we have a Global.asax and a Folder called “App_Start” which holds four different files such as BundleConfig, FilterConfig, RouteConfig and WebApiConfig. These play a very prominent role in deciding the Application’s control and flow.

Consider you are working as MVC Developer with WebDevTutorials. Our ASP.NET MVC application has a controller named EmployeeController. We want our EmployeeController to have a public non action method. What we will do to achieve this?

  • A. We can’t make a public method as non action in ASP.NET MVC.
  • B. Make return type void to a public method.
  • C. Add NonActionAttribute attribute to a public method.
  • D. Add VoidAttribute attribute to a public method.

For a complete FREE online test and MCSD Practice Exams on Web Technologies, Click Here.

 Correct Answer: C

Global.asax

Now let’s see deep into Global.asax file,ASP.NET MVC Global.asax
As you can see the Class is been inherited from System.Web.HttpApplication. As we know that we can handle the Application level events with the help of Global.asax even from the traditional ASP.NET it works as the same. Lets have a quick brush-up regarding those events.

  • Application_Start() – Triggers when the application starts for the first time
  • Application_Init() – Triggers when the application initialize for the first time
  • Application_BeginRequest() – Triggers each time a new request comes in
  • Application_EndRequest() – Triggers when request ends
  • Application_AuthenticateRequest() – Triggers when the request is to be authenticated
  • Application_Error() – Triggers when an application level error occurs
  • Application_End() – Triggers when the application times out or ended
  • Session_Start() – Triggers when there a user session starts
  • Session_End() – Triggers when the user’s session is ended

Here in sample ASP.NET MVC, we can see that Classes under App_Start folder is been invoked by passing necessary parameters.

Routing Module

RouteConfig is the most prominent Class involved in our Application life cycle’s first step, i.e handling the routes. UrlRouting Module is been informed about these Routes with the help of method called MapRoute().Register Routes in ASP.NET MVC

So basically what happens when the request comes in is, the RouteConfig find the matching route format from top and present that to RouteTable where it maps to the appropriate Controller.

Note: Route matching first will be taken in the RouteConfig file.

Controller Factory

The next vital step in MVC application life cycle is Controller factory, where if this factory receives the appropriate request from the RouteHandler, it generates the responding Controllers for the request. ControllerFactory creates the Controller Object and it implements the IControllerFactory interface.

When the mapping Controller object is created it comes to User defined Controller Class, for instance here we have the Book Controller class as a sample.Controller in ASP.NET MVC

Please note that, the BookController class implements Controller.Exam 70-486

Action Invoker in ASP.NET MVC Framework

The next important thing after creating Controller object is to, pick up the right ActionMethod based on the request parameter. ActionInvoker class is responsible for finding the method and invoke it.

ActionInvoker class implements the IActionInteface where we do have a method called InvokeAction.

Below screenshot represents some ActionMethods of the BookController class.ASP.NET MVC Action Invoker

  • Learn ASP NET MVC 5 step by step [Maruti Makwana, Corporate Trainer] 28 Lectures, 2.5 Hours Video, Intermediate Level
    Very easy to learn video series on Asp.Net MVC 5 Specially for those who are familiar with Asp.Net Web forms.
  • AngularJS for ASP.NET MVC Developers [Brett Romero] 10 Lectures, 1 hour video, Intermediate Level
    The Fastest Way For .NET Developers To Add AngularJS To Their Resume
  • ASP.NET with Entity Framework from Scratch
    [Manzoor Ahmad, MCPD | MCT] 77 Lectures, 10 hour video, All Level
    Latest approach of web application development
  • Comprehensive ASP.NET MVC [3D BUZZ] 34 lectures, 14 Hours Video, All Levels
    From zero knowledge of ASP.NET to deploying a complete project to production.

Action Result

As we are clear that, once the ActionMethod executes our logic we will return an ActionResult, based on the type of Result it returns, View generates accordingly.

Before that we do have some ActionFilter methods which is invoked based on ActionMethod’s life cycle. Some methods are such as OnActionExecuting, OnActionExecuted etc.

Let us see some of the ActionResult types as well,

  • ViewResult : Returns a view (html type page)
  • RedirectResult : Redirects to another action method
  • ContentResult : Returns Content (user defined)
  • JsonResult : Returns a serialized JSON data

View Renderer

The final and tangible step in MVC life cycle is rendering the View, where the user can see what he requested and get as a response. The ViewResult one of the ActionResult generates the appropriate View to the user, by parsing the Razor syntax (cshtml) and server side codes into HTML page to the Client.

ViewResult implements the IViewEngine Interface and has some view manipulating methods such as:

  • ReleaseView : To release the View
  • FindView : Maps the appropriate View
  • FindPartialView : Returns a Partial View

ASP.NET MVC Tutorial
We saw the internal workings of ASP.NET MVC application and how the control flows from IIS to View engine. Hope  his article helps you understand the Life cycle of MVC, kindly let us know if you have any questions.

Happy ASP.NET MVC Coding!

Advanced Web Developer Interview Questions and Answers Series: