ASP.NET Page Life cycle

ASP.NET Life cycle

At higer level, this is a two step process -

1. When user sends a request ASP.NET creates an environment which can process the request. Here ASP.NET creates instances of the core objects that will be used to process the requst. This includes HTTPContext, HttpRequest and HttpResponse object.

2. After creating the environment their is a series of events processed by modules, handlers and page objects to process the request.

(I) ASP.NET Environment Creations
1. User sends a request to IIS. IIS first checks which ISAPI (Internet Server Application Programming Interface) extension can server this request. If the request is for an .aspx page then the request will be redirected to ASP.NET (aspnet_isapi.dll) for processing.

2. If this is the first request to the website then a class called as "ApplicationManager" creates a application domian where the website can run.Application domain provides the actual isolation levels so that various website hosted in the same IIS Servers will not interfare with each other.

3. The newly created application domain creates hosting environment, i.e., the "HttpRuntime" object. Once the hosting environment is created, the necessary core ASp.NET objects like "HttpContext", "HttpRequest" and "HttpResponse" objects are created. The HttpContext objects contains reference to the HttpRequest and HttpResponse object. Browser related information and existing cookies are present in HttpRequest while HttpResponse object contains the information about the reponse that will be sent from server to the client i.e. cookies to be written etc.

4. Once all core ASP.NET objects are created, "HttpApplication" object is created. This objects contains the methods and events that are common for the application. If "global.asax" is present in file system then object of "global.asax" will be created. This file is inherited from "HttpApplication" class. Using global.asax we can handle all the events raised by HttpApplication object and perfrom our application specific operations.

5. Once HTTPApplication object is done with the request initialization, authentication and authorization events, then it is assigned to core ASP.NET objects to process the page.

6. HttpApplication then starts processing the request by HTTP module events, handlers and page events. This is the point where we write most of our code.