Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Global.asax application file in ASP.NET

Article:
Viewed:  1705 
Posted On:  07/12/2012 21:54:46 
What is Gloabl.asax, What it do....? 

 

In this artilce I am going to expalin what is Global.asax file. What is the role of Global.asax file in ASP.NET file.

What is Global.asax file: Global.asax allows us to write event handlers that react to global events in web applications.

When it called: Global.asax files are never called directly by the user, rather they are called automatically in response to application events.

Some Information about global.asax:

They do not contain any HTML or ASP.NET tags.
Contain methods with specific predefined names.
They defined methods for a single class, application class.
They are optional, but a web application has no more than one global.asax file.

How to add global.asax file in your project: 

Global.png 

Figure 1.

Now see events of Global.asax file

<%@ 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.
    }

</script>

Event that global.asax can handle:

1) Events that occurs only under specific conditions, i.e. request / response related events.
Order in which application event handler executes;

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

2. Application_AuthenticateRequest()
This method is called just before authentication is performed. This is time and place where we can write out own authentication codes.

3. Application_AuthorizeRequest()
After the user is authenticated (identified), it's time to determine the user's permissions. Here we can assign user with special privileges

4. Application_ResolveRequestCache()
This method is commonly used in conjunction with output caching. 

5. Application_AcquireRequestState()
This method is called just before session-specific information is retrieved for the client and used to populate the Session collection.

6. Application_PreRequestHandlerExecute()
This method is called before the appropriate HTTP handler executes the request.

7. Application_PostRequestHandlerExecute()
This method is called just after the request is handled.

8. Application_ReleaseRequestState()
This method is called when the session-specific information is about to be serialized from the Session collection so that it's available for the next request.

9. Application_UpdateRequestCache()
This method is called just before information is added to the output cache.

10. Application_EndRequest()
This method is called at the end of the request, just before the objects are released and reclaimed. It's a suitable point for cleanup code.


2) Events that don't get fired with every request.

1. Application_Start()
This method is invoked when the application first starts up and the application domain is created. This event handler is a useful place to provide application-wide initialization code. For example, at this point you might load and cache data that will not change throughout the lifetime of an application, such as navigation trees, static product catalogs, and so on.

2. Session_Start()
This method is invoked each time a new session begins. This is often used to initialize user-specific information. 

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

4. Session_End()
This method is invoked whenever the user's session ends. A session ends when your code explicitly releases it or when it times out after there have been no more requests received within a given timeout period (typically 20 minutes).

5. Application_End()
This method is invoked just before an application ends. The end of an application can occur because IIS is being restarted or because the application is transitioning to a new application domain in response to updated files or the process recycling settings.

6. Application_Disposed()
This method is invoked some time after the application has been shut down and the .NET garbage collector is about to reclaim the memory it occupies. This point is too late to perform critical cleanup, but you can use it as a last-ditch failsafe to verify that critical resources are released.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us