Hello Friends, In this tutorial, I am going to explain you ASP.NET MVC Page Life Cycle. While learning ASP.NET MVC it is important to know about Life Cycle of application from start to end. This is also mostly asked Interview Questions.  Let’s start the tutorial.

After completing this tutorial you will be able to understand:

  • RouteTable in ASP.NET MVC Routing.
  • ASP.NET MVC Page Life Cycle.

Introduction to ASP.NET MVC Page Life Cycle:

Before come to the main topic let’s  understand core concept about route table in Routing.

In ASP.NET there is always a one-to-one relationship between the URLS and the pages means there is always one URL for one page. But in the case of ASP.NET MVC, there is a many-to-one relationship between URL and pages. i.e. multiple URL can be mapped to one controller. For example.

  1. http://mydomain/Employee/1
  2. http://mydomain/Employee/2
  3. http://mydomain/Employee/3

The ASP.NET has RouteTable to map the particular controller to particular URL. A project can have only one Route Table. The Route Table is initiated in the Global.asax.

Life Cycle

The following are the Life Cycle of ASP.NET MVC:

Step 1- Register Route Table.

When an ASP.NET MVC application starts at the first time the first thing that happens is to fill the route table. The Routing System decides what to do if any matching pattern found in the Route Table. So in this step, the incoming URL is mapped to the registered URL.

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

Step 2- Retrieve  Route.

If any matching URL is found the URLRoutingModule created the RouteData. This RouteData Contains the information for Controller and Action.

Step 3- Create RouteContext:

If URLRoutingModule successfully retrieves the RouteData the next step is to create RouteContext Object. This RouteContext object represents the current HTTPContext and RouteData.

Step 4- Create Controller Instance.

After the successful creation of a RouteContext object that comes in front is to create the instance of the Controller. This Requested object is sent to MVCHandler to create an instance of the Controller class. This MVCHandler is responsible for executing the real processing of the application. MVCHandler implements IHTTPHandler interface that contains the ProcessRequest method to process a further request. When an instance of the Controller class is created it calls the execute method of the Controller class.

The second phase of this step in MVC Life Cycle is to send the final response as an output to the View.

protected internal virtual void ProcessRequest(HttpContextBase httpContext)
{
SecurityUtil.ProcessInApplicationTrust(delegate {
IController controller;
IControllerFactory factory;
this.ProcessRequestInit(httpContext, out controller, out factory);
try
{
controller.Execute(this.RequestContext);
}
finally
{
factory.ReleaseController(controller);
}
});
}

Step 5- Invoke Action:

After the instance of the Controller class is created the next major step is to choose right Action method. The ActionInvoker class is responsible for choosing right Action method and executing. The ActionInovker class implements IActionInterface that has InvokeAction method.

Step 6- Return Result.

After the execution of action method in the previous step, the action method prepares the appropriate result and return the response data. This response data can be of any type such as ViewResult, ContentResult, JsonResult etc.

Conclusion:

Hope you understand the Life Cycle of ASP.NET MVC. I would like to have your feedback, Comments and your suggestions.

View More:

Thank You.

Leave a Comment