Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Saturday, July 7, 2012

global.asax file in Asp.Net

In this article we will discuss what is global.asax file and what is the use of global.asax in Asp.Net application. Also you can check my last article on Web.config and Machine.config in

- Global.asax file is useful to write global event handlers. Global.asax file executes its code automatically in response to some global events.
- A web application can not have more than one global.asax file, but its optional means a web application may not contain a global.asax file.

- We can add global.asax file from Visusal Studio 2010 by Add -> New Item and then chose Global Application Class template.

- By default some application events were inside the global.asax file like below:

<%@ Application Language="C#" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e)
        // Code that runs on application startup
    void Application_End(object sender, EventArgs e)
        //  Code that runs on application shutdown
    void Application_Error(object sender, EventArgs e)
        // Code that runs when an unhandled error occurs
    void Session_Start(object sender, EventArgs e)
        // Code that runs when a new session is started
    void Session_End(object sender, EventArgs e)
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

- we only need to write the code for the corresponding events.
- Global.asax doesn’t contain any HTML or ASP.NET tags.

- Every global.asax file defines the methods for application class.

- Some of the events that usually appears in global.asax are:

Application_BeginRequest(): This method is called at the start of every request.

Application_AuthenticateRequest(): This method is called just before authentication is performed.

Application_EndRequest(): This method is called at the end of the request, just before the objects are released.

Application_Start(): This method is invoked when the application first starts up and the application domain is created.

Session_Start(): This method is invoked each time a new session begins.

Application_Error(): This method is invoked whenever an unhandled exception occurs in the application.

Session_End(): This method is invoked whenever the user’s session ends.

Application_End(): This method is invoked just before an application ends.

0 on: "global.asax file in Asp.Net"