The ASP.NET application lifecycle consists of various stages and events. Here we will see some of the important stages in the application life cycle.
The following are some of the main stages in the application life cycle:
- When a client such as a web browser requests an ASP.NET resource, the request is received by a web server (like IIS). IIS has certain file name extensions mapped to different ISAPIs. There are various ISAPIs that handle different file name extensions. ASP.NET is one such ISAPI that handles certain file name extensions such as .aspx.ascx and .asmx.
- When the first request for a resource is received by ASP.NET, ASP.NET creates an AppDomain to execute the application.
- Once the appdoamin has been created, the main objects like HttpRequest and HttpResponse are created. The HttpContext class is also initialized and has references to current request objects like HttpRequest and HttpResponse
- The HttpApplication class object is created, if the global.asax file in the application then it represents a class derived from the HttpApplication class and the instance of this derived class is created instead. This instance of the application class is responsible for handling the request. So there is a different instance of the HttpApplication class to handle a request.
- Also at this time the standard modules are created.
- The Init method of the application instance is called.
Now when the application executes, certain events are fired in the application class.
This life cycle is extensible using the modules that are classes that implement the IHttpModule interface. There are several built-in classes that implement this interface, such as the SessionStateModule.Modules raises certain events that we can handle in the Global.asax file. The module events in the glabal.asax file follow the naming convention modulename_event like Session_OnStart and Session_OnEnd.
We can create custom modules if the built-in modules do not meet our requirements. One of the common scenarios for implementing a new module is to implement the logging functionality in an ASP.Net application. So we can create a module that logs the information whenever the application resource is requested or a specific response is returned to the browser.
"Application_Start" and "Application_End" are two of the main events raised in the application life cycle. Application_Start is raised once in the application lifecycle when the request for the first resource is made. Application_End is also raised once in the application life cycle just before the application is unloaded. We can handle both of these in the global.asax.
Also there is the Init() method that is raised once for every HttpApplication instance when it is initialized.
Apart from these modules, certain events are fired in the code behind page's also, like the Init, Load, Validate, Render and Unload. These page events, along with the events in the application life cycle (handled in the global.asax), allow us to handle various stages and application requirements.