The Global.ASAX File
Published: 02 May 2004
Unedited - Community Contributed
ASP.NET has a match for the Classic ASP Global.asa - Global.ASAX. This article will go over the advancements and usage of global.ASAX
by . .
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32280/ 65



ASP.NET has a match for the Classic ASP global.asa file and that is (following with .aspx tradition - Global.asax. If you remember what global.asa does, you pretty much have sorted what global.asax does. This article will go over that as well as explain some of the new and extended features that it comes with.

Overview of Global.asax

Global.asax is a file that resides in the root directory of your application. It is inaccessible over the web but is used by the ASP.NET application if it is there. It is a collection of event handlers that you can use to change and set settings in your site. The events can come from one of two places - The HTTPApplication object and any HTTPModule object that is specified in web.confg or machine.config. We will be covering both here.

In Global.asax there are a lot more events to work with (and you can create your own) than there are in global.asa which gives you a lot more customization when it comes to your application.

Exposed Events

Exposed Events

The HTTPApplication class exposes the following events for you to handle.

  • AcquireRequestState
  • AuthenticateRequest
  • AuthorizeRequest
  • BeginRequest
  • EndRequest
  • Error
  • PostRequestHandlerExecute
  • PreRequestHandlerExecute
  • PreSendRequestContent
  • PreSendRequestHeaders
  • ReleaseRequestState
  • ResolveRequestCache
  • UpdateRequestCache

And that's just the HTTPApplication class! You can see that there are loads of events there for you to choose from (and some that you will never use).

Remember that it will automatically search for an event handler like -

Application_EventName(sender as object, e as EventArgs)

And you also still have Session_OnStart and Session_OnEnd, you also can still use <object runat="server" ...> tags like in ASP.

Handling the Events

Handling the Events

Let's take a look at a sample Global.asax file -

<%@ Import Namespace="System.Diagnostics" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Started!", EventLogEntryType.Information)
End Sub

Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Session.Contents.Add("TimeStart", DateTime.Now())
End Sub

Application_Error(ByVal sender As Object, ByVal e As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Error Occured!", EventLogEntryType.Error)
End Sub

Application_End(ByVal sender As Object, ByVal e As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Ended!", EventLogEntryType.Warning)

As you can see, this handles four events.

When the application starts or ends, it writes a semi-informative message to the system Event Log.
On an error, it writes an almost-useless message to the EventLog.
When a session begins (basically whenever someone visits the site) it adds a new session variable with the time they logged in (possibly a waste of session space).

Notice how it looks almost like a normal ASP.NET page, but without the HTML content.

ASP.NET will automatically compile and process this file.



If you've been reading in the Related Articles you will know that HTTPModules can expose events and these events can be handled in global.asax.

Here I'll just give you a quick module and handler, but you will be able to see the advantages of it.


Here is our sample HTTPModule -

Imports System

Public Class QuickModule : Implements IHttpModule

Public Event tester As EventHandler

Public Sub Dispose() Implements IHttpModule.Dispose

Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
application.BeginRequest, AddressOf

Public Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
, EventArgs.Empty)

End Class

The bit from the web.config file -

add name="QuickModule" type="HttpModHan.QuickModule, HttpModHan" />

As well as a bit from the global.asax file -

Sub QuickModule_tester(ByVal sender As Object, ByVal e As EventArgs)


I don't think that there is any need for a demo here because the result is obvious. You can see that all we did to handle it was to put -

ModuleName_EventName(sender as object, e as EventArgs)

And ModuleName is the name that you defined in web.config.


Well, that's Global.asax for you. As you can see, it's a hell of a lot more powerful now than in Classic ASP and the fact that you can now develop your own events to handle in there just makes it all that more important and useful to your application. Check the Related Articles for more information on HTTPModules if you haven't heard of them before or want more information on them.

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-03-07 4:02:45 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search