Working with Global.asax file and Application Events in Web Application Development

The global.asax file allows you to write code that responds to global application events. These events fire at various points during the lifetime of a web application, including when the application domain is first created (when the first request is received for a page in your website folder).

 To add a global.asax file to an application in Visual Studio 

choose Website , Add New Item, and

select the Global Application Class file type. Then, click OK.

The global.asax file looks similar to a normal .aspx file, except that it can't contain any HTML or ASP.NET tags. Instead, it contains event handlers that respond to application events. When you add the global.asax file, Visual Studio inserts several ready-made application event handlers. You simply need to fill in some code.

 For example, the following global.asax file reacts to the EndRequest event, which happens just before the page is sent to the user:

 

<%@ Application Language="C#" %>

<script language="c#" runat="server">

 protected void Application_EndRequest(object sender, EventArgs e)

{

Response.Write("<hr />This page was served at " +

DateTime.Now.ToString());

</script>

 

This event handler uses the Write() method of the built-in Response object to write a footer at the bottom of the page with the date and time that the page was created. Each ASP.NET application can have one global.asax file. Once you place it in the appropriate website directory, ASP.NET recognizes it and uses it automatically.

But Using this technique—responding to application events and using the Response.Write() method—isn't the best way to add a footer to the pages in your website. A better approach is to add a user control that creates the footer.

Additional Application Events :

Application.EndRequest is only one of more than a dozen events you can respond to in your code. To create a different event handler, you simply need to create a subroutine with the defined name.

Basic Application Events                    Event Handling Method Description

Application_Start() : Occurs when the application starts, which is the first time it receives a  request from any user. It doesn't occur on subsequent requests. This event is commonly used to create or cache some initial information that will be reused later.

 

Application_End() :  Occurs when the application is shutting down, generally because the web server is being restarted. You can insert cleanup cod here.

Application_BeginRequest() : Occurs with each request the application receives, just before the page code is executed.

 

Application_EndRequest() :  Occurs with each request the application receives, just after the page code is executed.

 

Session_Start() : Occurs whenever a new user request is received and a session is started.

Session_End() :  Occurs when a session times out or is programmatically ended. This event is only raised if you are using in-process session state storage (the InProc mode, not the StateServer or SQLServer modes).

 

Application_Error() :  Occurs in response to an unhandled error.