ASP.NET Application Life Cycle
Working with ASP.Net applications requires working with the application's life cycle and handling the appropriate events in the application's lifecycle.
Working with ASP.Net applications requires working with the application's life cycle and handling the appropriate events in the application's lifecycle. Page events, HTTPHandlers and HTTPModules are some of the common places we can look to handle various application life cycle scenarios. In the following article we will see each of these.
When an ASP.NET page executes, it undergoes a life cycle that includes various stages. The important stages in the page life cycle are Initialization, Load, Control Events, Rendering and Unload. The important features of each of these stages are:
Initialization: During this stage we can access the controls on the page but the control's viewstate has not yet been initialized.
Load: In case of a postback, the control's properties have been initialized from the viewstate.
Control Events: In case of a postback, the control's events are called.
Rendering: During the rendering stage, the page calls the Render method for each control and thus each control generates the HTML output that is merged with the resulting HTML of the page.
Unload: The Unload event is raised after the page has been fully rendered. In this stage the generated page output has already been sent to the client and the page can be discarded.
Each of the stages mentioned above raises an event that we can handle in our page's code behind. Pages have a feature called automatic event wireup in which methods with specified names are automatically executed when a particular event occurs such as "Page_Load" and "Page_Init". We can enable or disable AutoEventWireup using the AutoEventWireup attribute of the "@Page" directive.
Some of the most frequently used page events and their uses are:
PreInit: Setting a master page or setting the Theme property.
Init: Read or set control properties.
Load: Set properties of controls and to establish database connections.
Control events: Handle specific control events. We can bind control events either in code or declaratively using attributes.
Render: This is mostly used for custom controls to generate the control's output.
Unload: Final cleanup for controls like closing database connections.
HTTP Handler is a type that runs in response to a request made to an ASP.NET application. A request can be for any type of ASP.NET resource but it is the HTTPHandler that generates the response. One of the most common handlers is the page handler that runs in response to a request made for an .aspx page.
Another common handler is a web service handler that runs in response to a request for a .asmx web service. Generic web handler (.ashx) is the default web handler and is usually used when we create a custom web handler.
We can also create custom handlers. To create a custom handler we implement the IHttpHandler interface. We need to implement two members of this interface. The "IsReusable" property and the "ProcessRequest()" method. The IsReusable property specifies whether a new instance of the handler is created every time or an existing instance can be reused. The ProcessRequest() method is where we put the actual code of the handler to produce the desired output from the handler.
The handler also has access to session state and application state. When the handler is called, the ProcessRequest() method is executed and returns the resulting output to the requesting browser. We can use the file name extension .ashx for our custom handlers since that file extension is already registered with IIS. If we want to use some other extension we need to register it with IIS.
HTTPModules are assemblies that are executed for each HTTPRequest that is made. HTTPModules are used to examine incoming requests and responses. Also HTTPModules can modify the request and response. There are several builtin modules, like SessionState and Authentication.
Also we can implement our own custom modules.To create our custom modules we use a class that implements the "IHttpModule" interface and register the module in the web.config file's httpModules section. In the custom module class we will implement the init() method and inside that method we will bind the eventhandler with the application event that we want to handle. Some of the application events we can bind to eventhandlers in our custom module are:
The following example binds the event handler with the BeginRequest application event:
public void Init(HttpApplication appObj)
appObj.BeginRequest += new EventHandler(OnBeginRequest);
Using Page-Events, HTTPHandlers and HTTPModules we can handle many of the scenarios related to the ASP.Net application life cycle.