Published:
02 May 2004
|
Abstract
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 . .
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
28433/
48
|
|
|
Introduction |
Introduction
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
Sub Session_Start(ByVal
sender As
Object,
ByVal e
As EventArgs)
Session.Contents.Add("TimeStart", DateTime.Now())
End
Sub
Sub Application_Error(ByVal
sender As
Object,
ByVal e
As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Error Occured!",
EventLogEntryType.Error)
End
Sub
Sub Application_End(ByVal
sender As
Object,
ByVal e
As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Ended!",
EventLogEntryType.Warning)
End
Sub
</script> |
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.
|
HTTPModules |
HTTPModules
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
Imports System.Web
Public
Class
QuickModule : Implements
IHttpModule
Public
Event tester
As
EventHandler
Public
Sub Dispose()
Implements
IHttpModule.Dispose
End
Sub
Public
Sub Init(ByVal
application As
HttpApplication) Implements
IHttpModule.Init
AddHandler application.BeginRequest,
AddressOf
OnBeginRequest
End
Sub
Public
Sub
OnBeginRequest(ByVal
sender As
Object,
ByVal e
As
EventArgs)
RaiseEvent tester(Me,
EventArgs.Empty)
End
Sub
End
Class |
The bit from the web.config file -
<httpModules>
<add
name="QuickModule"
type="HttpModHan.QuickModule,
HttpModHan"
/>
</httpModules> |
As well as a bit from the global.asax file -
Sub QuickModule_tester(ByVal
sender As
Object,
ByVal e
As EventArgs)
Response.Write("EVENT HANDLER TRIGGERED!!!")
End
Sub |
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.
Summary
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
|
|