Monday, April 20, 2020

ASP.NET MVC Life Cycle

In ASP.NET MVC application, when a client makes a request, then that request goes through various stages before returning the response to the client. The various stages the request goes through are nothing but the ASP.NET MVC Request Life Cycle or you can say ASP.NET MVC Request Pipeline. So, if you are working as an ASP.NET MVC developer, then you should be aware of the ASP.NET MVC Request Life Cycle i.e. from birth to death of a request. As part of this article, we will learn two things. They are as follows:

  1. Application Life cycle
  2. Request Life Cycle
Note: In many articles on the web, you can find what they are saying both are the same. But in reality, both are not the same. Let discuss what exactly they are.
Application Life Cycle of ASP.NET MVC Application:
The Application Life Cycle and Request Life Cycle both are not the same.
The Request Life Cycle in ASP.NET MVC application starts when a request is made for a resource (or you can say for a page) from the client and it ends when the response is sent back to the client and this will happen for each and every request made by a client. On the other hand, the Application life cycle in ASP.NET MVC application starts when the first request comes to the Application from a client.
ASP.NET MVC Application Life Cycle
Explanation of the above Architecture:
As you can see in the above image, when the client made the first request, then the Application_Start() method of the Global.asax.cs class file is going to be fired. The point that you need to remember is, this is the first method of your ASP.NET MVC application that is executed when the application starts.
The Application_Start event is used to set up the initial required configuration needed in order to run the application such as Registering the Routes, Registering the Filters, Areas and Bundle Configurations, etc. But here in this article, we are only interested in the RegisterRoutes method.
The Application_Start() method calls the static RegisterRoutes Method of the RouteConfig class by passing the RouteCollection (i.e. RouteTable.Routes) as a parameter as shown below.
Application Start Event in ASP.NET MVC Application
If you go to the definition of RouteTable class then you will see that the Routes is nothing but a static property of type RouteCollection as shown in the below image.
Route Table in ASP.NET MVC Application
Once the Application_Start event calls the RegisterRoutes Method of the RouteConfig class, then the RegisterRoutes method fills the Route Table (i.e. comes as a parameter to RegisterRoutes method) with the routes defined for your application as shown in the below image.
Route Config in ASP.NET MVC Application
The Application_End event of Global.asax.cs file is going to be fired when the Web Server Recycles the application pool on which it is hosted or when the CPU or memory thresholds are exceeded. The point that you need to remember is, once the Application_End event is fired, then the next request coming to the application will be treated as the first request and again the Application_Start event is fired and all the above steps we discussed are going to happen in order.
ASP.NET MVC Request Life Cycle
Now let us understand the Request Life Cycle of an ASP.NET MVC application. Please have a look at the following diagram which shows a high-level architecture of the ASP.NET MVC Request Processing Pipeline.
ASP.NET MVC Request Life Cycle
When the client makes a request, and if it is the first request to the application then the Application_Start event is going to be fired and what these events do internally we already discussed at the start of this article. So, in short, the Application_Start event calls the RegisterRoutes Method of the RouteConfig class which will fill the RouteTable with the routes defined for your application. Then the Request comes to the Routine module.
Routing in ASP.NET MVC Request Life Cycle:
The aim of the Routing Module in ASP.NET MVC Application is very simple. It just maps the incoming request i.e. URL to a Route and then selects the HttpHandler which is associated with the Route to handle that Request. Please have a look at the following diagram to understand How the Routing Module works in ASP.NET MVC Application.
Routing in ASP.NET MVC Request Life Cycle
As shown in the above image, we can say that the Routing Module is the first module in the ASP.NET MVC Request Processing Pipeline which receives the incoming request made by the client. Once it receives the incoming request, then it tries to find a matching URL pattern defined in the Route Table. If it does not find any matching URL pattern in the Route table then it simply returns 404 HTTP Status code to the client.
On the other hand, if it found a matching URL Pattern in the Route table, then it selects the HTTP Handler (a handler which implements the IHttpHandler interface) which is associated with that Route and then forward that request to the corresponding handler.
The Routeing Module is also responsible for creating the RequestContext object. And also responsible for passing that RequestContext object to the corresponding Handler. The default handler used by an ASP.NET MVC application is MvcHandler. But if you want then you can change this default handler that we will discuss in our upcoming articles.
Note: The RequestContext object created by Route Module is available in each and every stage of the ASP.NET MVC Request processing Pipeline.

No comments:

Post a Comment