Powered by Blogger.

Saturday, March 1, 2014

global.asax file in Asp.Net

In this post we will discuss about what is the use of global.asax file in Asp.net.

You can also check my previous posts on:
- Sealed class and Sealed method in C#.Net

- How to make fade out animation effect in asp.net using Ajax AnimationExtender Control?

- Write exception to Event Log in Asp.Net C#.Net

- global.asax file is responsible for writing code for global events. These events fire at various points during the lifetime of a web application, including when the application domain is first created.

- There can be only one global.asax file in a Asp.Net application. You can add it through visual studio, Right click on the web site -> Add New Item and then select Global Application Class file type.

- It can’t contain any HTML or ASP.NET tags, it contains event handlers that respond to application events.

- Some of the global events are:
Application_Start(): Occurs when the application starts, which is the first time it receives a request from any user.

Application_End(): Occurs when the application is shutting down, generally because the web server is being restarted.

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.

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

<%@ Application Language = "C#" %>
<script language = "c#" runat = "server">
protected void Application_EndRequest(object sender, EventArgs e)
 //You can write code here.