View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

MVC Page Life Cycle Explained in Simple Language

By Sriram

Updated on Nov 17, 2022 | 13 min read | 9.4k views

Share:

This article will focus on the lifecycle of the MVC application and how the application processes a request through its passing through the application’s components. Two life cycles are present in an MVC life cycle. These two life cycles are:

  • The application life cycle
  • The request life cycle. 

Check out our free courses to get an edge over the competition

Application life cycle

Application life cycle generally means the time at which the process of the application begins to run. To denote the MVC application life cycle, the start and the end of the application are marked. 

Check out upGrad’s Advanced Certification in Cloud Computing 

The request life cycle

The request life cycle means the series of events that happen whenever the application handles an HTTP request. Routing is the entry point for beginning any application of the MVC. When a request is made to the ASP.NET platform, the decision is taken on how to handle the request through the routing module. The term module means components of .NET, which are used for adding functionality into the code. It is the responsibility of the routing modules to match the URL that is incoming to the respective routes defined in the application. A route handler is associated with every route, which marks the point of entry into the framework of MVC. 

The framework of MVC is responsible for handling the conversion of the route data into controllers that will further handle the requests. Once the creation of the Controller is done, the next step is taking action. This is done through the component of action invoker, which selects an action method to invoke the Controller. 

Check out upGrad’s Advanced Certification in Cyber Security

Once the preparation for the action result is done, the next step is the result execution. A step of separation is created for the result from the result execution. For a view type result, the application will call the view engine, and it will find and help in rendering the view. If the result is not a view, execution of the action will take place on its own. It is the result execution that generates the actual response towards the original HTTP request.

Most of the components are known by the developers that they are used to process the request. In most cases, the methods and controllers related to the actions are being worked upon. Also, different action results and views are being worked upon. But there are other components too within the framework of MVC. There might be many questions in one’s mind about how the flow of requests is carried out through the different components or the HTTP module role and the handler while processing the request. The MVC framework being a framework of web development, there have to be modules of the HHTP and the HTTP handlers in the MVC pipeline of the framework. 

Many components are present in the MVC page life cycle, apart from the Controller and the action methods. 

UrlRoutingModule 

This is the start of an MVC life cycle.This is a type of HTTP module. Whenever a request is first made, it is intercepted through the UrlRoutingModule. In this module, it is being decided upon whether the MVC application should handle the request. The UrlRoutingModule selects the first route that matches.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

How is the request matched with those routes present in the MVC application through the UrlRoutingModule?

Looking at the method of RegisterRoutes, which is called from global.asax, the routes added to the RouteCollection of the routes could be seen. Calling of the method is done from the event handler at the application_start of global.asax. This is a very important part of the MCV page life cycle. 

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

How are the routes in the RouteHandler defined?

The routes are defined as extension methods, as it could be seen in the method of the maproute. The route handler is associated with all routes.

The method of the UrlRoutingModule is defined in the following way.

Source

Through the above explanation, it can be known that the UrlRoutingModule knows all the routes that are defined in the application. Therefore, the correct route can be matched with the request in the application. 

An important point to be noted is that the module UrlRoutingModule is involved in selecting the first matching route. So whenever the match has been detected, the process of scanning stops. If we summarize the thing that happens at this stage, the route handler is attached to the routes through the URLRoutingModule.

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

RouteHandler

Through the method of the maproute, the routes get attached to the instances of the MvcRouteHandler. The interface IRouteHandler is implemented through the MvcRouteHandler. 

Therefore, the object of the MvcRouteHandler is used for obtaining a reference for the thing of the MvcRouteHandler that is the application’s HTTPHandler. 

Creation of the MvcRouteHandler, demands the calling of the method PostResolveRequestCache(). This method PostResolveRequestCache() can be defined in the following way:

Source

Therefore, the following events occur at the PostResolveRequestCache() method.

  • A GetRouteData() method is present in the RouteCollection property. The method is called and passed through the HttpContext.
  • RouteData object is returned through the method of GetRouteData().
  • a RouteHandler property is contained by the routeData that returns the handler IRouteHandler for the request, which is current, i.e., the MvcRouteHandler.
  • The method GetHttpHandler() is associated with the MvcRouteHandler that returns a reference for the MvcHandler.
  • Controls are then delegated to the instance of MvcHandler.

MvcHandler

The MvcHandler is a normal handler of the HTTP.  It is a very important part of the MVC life cycle. As it is a handler of the HTTP, the implementation of the method ProcessRequest() is carried out. This method ProcessRequest() can be defined as:

Source

The method ProcessRequest() is used for calling the method ProcessRequestInit() as defined in the below image:

Source
The following events occur at the method ProcessRequest().

  • The ControllerFactory is created through the calling of the method ProcessRequestInit().
  • The ControllerFactory creates the Controller.
  • The Execute() method of the Controller’s is called.

ControllerFactory

One can observe that the ControllerFactory is obtained through the ProcessRequest() method. The ControllerFactory is then used to create the object of the Controller object. The interface IControllerFactory is implemented by the Controller factory. This is a major step in the life cycle of MVC. 

BY DEFAULT, the MVC framework creates the type DefaultControllerFactory type when the ControllerBuilder is used for creating the ControllerFactory.

The following line of code creates the ControllerFactory in the method ProcessRequestInit().

Source

Controller

It has been observed that the Controller object is created in the ControllerFactory in the method ProcessRequest() of the MvcHandler. The created Controller contains the methods for the actions that are to be taken by the framework. Whenever a request of URL is made in the browser, there is a calling of the method for the actions. The controllers are created so that there is no explicit implementation of the interface of the IControlle. The class Controller is used for creating the controllers that provide several features. The class of Controller can inherit another class of Controller which is called the “ControllerBase” as defined in the code shown below:

Source

The ActionInvoker is used to call the methods of the actions in the Controller. It is one of the components of the MVC page life cycle.

The following events occur after the creation of the Controller through the controller factory:

  • Calling the method Execute() of controllerbase.
  • The method ExecuteCore()is called by the method Execute() that is declared as abstract and is then defined through the class Controller.
  • The action name is retrieved from the RouteData through the method ExecuteCore() by the class Controller.
  • The method ActionInvoker’s InvokeAction() is called by the method ExecuteCore().

ActionInvoker

It is used for the selection of actions.

The class ActionInvoker has some important responsibilities that include finding an action method and then invoking the method of action in the Controller.

The events that occur when the method ActionInvoker’s InvokeAction() is called are:

  • The information regarding the Controller has to be obtained by the ActionInvoker and the action that is needed to perform. 
  • The descriptor object’s provides this information.
  • The controller name and the action name are provided by the descriptor class of the action and Controller.
  • There is invoking the ActionMethod .

ActionResult

The ActionResult is a type of the abstract class. Being an abstract method, there are different implementations for the method ExecuteResult() provided by the different sub-classes.

The following events occurs in the ActionResult

  • There is an invocation of the methods of OnActionExecuting of the ActionFilters.
  • Invoking of the method action.
  • Invoking of the methods OnActionExecuted methods of ActionFilters.
  • There is a return of the ActionResult from the ActionMethod
  • Calling the method ExecuteResult() of the ActionResult.

ViewEngine

Most of the applications use ViewResult as their return type. A view is rendered through the ViewEngine to the client. From this view, the HTML can be generated. The HTML is generated from the view through the ViewEngine.

Benefits of Using Page Life Cycle of MVC

The life cycle of MVC and its usage comes with many benefits. This is an important page life cycle in MVC that can be used to develop website applications more efficiently. Let’s check out a few benefits of MVC page life cycle. 

  • Assembling large applications – It is incredibly simple to split and arrange the content on web apps into a wide range of applications due to the three layers of code separation (generally managed by larger developer teams). The main benefit of employing such coding conventions is that they make adding new features easier and discovering certain areas of code easily.
  • AMI or Asynchronous Method Invocation is supported – It is not surprising that the MVC design enables the usage of  (AMI), enabling developers to create web-based applications that execute more quickly, given how effectively JavaScript and its libraries integrate with it. It implies that MVC apps may be configured to function with site-specific browsers, PDF files, and desktop applications as well.
  • Simple planning and upkeep – The MVC paradigm is beneficial during the early planning stages of the application since it provides the developer with a roadmap on how to organize their concepts into application code. Additionally, it is a fantastic tool for reducing code duplication and simplifying application management.
  • The development process is faster- Since the code is divided into three layers, creating web applications using the MVC model enables two developers to work on different sections of the program simultaneously, such as the view and controller. This makes applying logic simple and helps double the development process speed. The MVC paradigm has resulted in a fast turnaround speed compared to other agile practices.
  • A platform that Supports SEO – The MVC framework offers tremendous assistance for the creation of web applications that support SEO. The page life cycle in MVC offers a simple method for creating RESTful URLs that are SEO-friendly in order to increase traffic from a certain application.
  • Encourages test-driven development – The MVC pattern has the significant benefit of greatly streamlining the testing procedure. Large-scale programs can be more easily debugged since the application’s numerous tiers are well defined architecturally and appropriately structured. hence making the use of unit tests during application development simple.
  • Provides data that is not formatted – The MVC framework gives you the ability to build your own view engine by delivering data that is not structured. For instance, you can format any sort of data using HTML; however, with an MVC model, you can also format the data using the Macromedia Flash or Dream viewer. The fact that identical elements may be used with either interface is advantageous for developers.
  • Different viewpoints – The MVC design makes it simple to create several view elements for your modeling framework. It gives you the ability to create several view components, preventing code duplication by separating data from logic. 
  • Simple to Modify – The MVC technique makes it simple to transform the entire app. The MVC pattern streamlines the process of adding and modifying new types of views.  Therefore, changes made to one part of the app will never change the architecture as a whole. As a result, the application’s adaptability and durability will be improved.

Conclusions

Understanding the role of every component in the application is a crucial step. This way, the connections between the components and their way of working can be understood. If you want to be an experienced developer, then mastering your skills is one way. You can check out the Online Software Engineering Courses of upGrad which will give you certification and expert training in the area of developing applications and software. The course is designed for both male and female working professionals and is certified from IIIT-B, and you will get their Alumni Status.

Frequently Asked Questions (FAQs)

1. Can you explain the page life cycle of MVC?

2. How can we maintain sessions in MVC?

3. How many types of filters are there in MVC?

Sriram

171 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program