ASP.NET Application Life Cycle and Page Life Cycle

2011-08-10


ASP.NET Application Life Cycle:

The life cycle of an ASP.NET application starts with a request sent by a browser to the Web server (IIS).

ASP.NET Application Life Cycle can be different due to different version of web server: IIS7.0 is different from IIS 6.0 / 5.0.

The main difference in processing stages between IIS 7.0 and IIS 6.0 is in how ASP.NET is integrated with the IIS server. In IIS 6.0, there are two request processing pipelines. One pipeline is for native-code ISAPI filters and extension components. The other pipeline is for managed-code application components such as ASP.NET. In IIS 7.0, the ASP.NET runtime is integrated with the Web server so that there is one unified request processing pipeline for all requests.

IIS 6.0/5.0:

1: User requests an application resource from the Web server.

IIS (Web Server) received the request and check the server extension is .aspx? if yes, IIS maps the request to aspnet_isapi.dll file. the ASP.NET worker process is loaded now;

2: ASP.NET receives the first request for the application.

ApplicationManager class creates an application domain. and an instance of HostingEnvironment class is created within this domain, which provides access to information about the application such as the name of the folder where the application is stored.

3: ASP.NET core objects are created for each request.

4: An HttpApplication object is assigned to the request. Here an instance of the HttpApplication class is created or a Global.asax class is derived from the HttpApplication class if  the application has Global.asax file.

5: The request is processed by the HttpApplication pipeline. A series of  events are executed by the HttpApplication class here such as  BeginRequest event and AuthenticateRequest event, etc;

(please read more detail information from MSDN ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0)

IIS 7.0:

1: A request is made for an application resource;

2: The unified pipeline receives the first request for the application, an instance of the ApplicationManager class is created, which is the application domain that the request is processed in. Application domains provide isolation between applications for global variables and enable each application to be unloaded separately. An instance of the HostingEnvironment class is created, which provides access to information about the application, such as the name of the folder where the application is stored.

3: Response objects are created for each request;

4: An HttpApplication object is assigned to the request;

5: The request is processed by the HttpApplication pipeline. similar with IIS 6.0/5.0, a series of events are executed here.

(please read more detail information from MSDN ASP.NET Application Life Cycle Overview for IIS 7.0)

 

ASP.NET Page Life Cycle:

An ASP.NET page goes through a life cycle in which it performs a series of processing steps, for example initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.

Common Page Life Cycle Stages:

1: Page request: The page request occurs before the page life cycle begins.

2: Start:  Page properties are set such as Request and Response. and page check IsPostBack and UICulture propertiese

3: Initialization: controls are available and control's UniqueID property is set. A master page and themes are also applied;

4: Load: if the current request is a postback, control properties are loaded  from view state and control state.

5: Postback event handling: Control event handlers are called

6: Rendering: Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control;

7: Unload: After the page has been fully rendered and sent to the client, a Unload event is raised, page is ready to be discarded.

Page Life Cycle Events:

PreInit  Init
InitComplete
PreLoad
Load
Control events
LoadComplete
PreRender
PreRenderComplete
SaveStateComplete
Render
Unload

(More detail information please read MSDN: ASP.NET Page Life Cycle Overview )

Also, there is another perfect and detail explanation article about ASP.NET Application and Page life cycle.