It is important for developers to understand the ASP.NET web application life cycle so that the developers can write code at the appropriate life cycle stage for the intended effect.
The life cycle of an ASP.NET application starts with a request sent by a browser to the Web server (typically IIS).
ASP.NET is an ISAPI extension under the Web server. When a Web server receives a request, it examines the file name extension of the requested file,
determines which ISAPI extension should handle the request, and then passes the request to the appropriate ISAPI extension.
ASP.NET handles file name extensions that have been mapped to it, such as .aspx, .ascx, .ashx, and .asmx.
The following figure shows the web application initialization and how individual requests are handled (Move the cursor around to see more explanations).
- ASP.NET web application initialization: when ASP.NET receives the very first request, it will go through the following steps:
ApplicationManager creates an application domain.
Winthin an application domain, an instance of the HostingEnvironment class is created.
HttpRuntime object created.
See the (a) in the figure above.
- Individual request handling
See the (b) in the figure above.
- A group of core objects are created and initialized: HttpContext, httpRequest, and HttpResponse.
- An instance of HttpApplication class is created, or
The HttpRuntime class picks up an HttpApplication object from an internal pool and sets it to work on the request.
- The instance of HttpApplication executes the applications events defined in the Global.asax such as BeginRequest.
- Page Life Cycle: Based on the file name extension of the requested resource, select a class that implements IHttpHandler to process the request.
- The instance of HttpApplication class continues to execute the application events defined in the Global.asax such as PostMapRequestHandler.
When ASP.NET receives the first request for any resource in an application, The class ApplicationManager creates an application domain.
Application domains provide isolation between applications for global variables and allow each application to be unloaded separately.