by . .
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
25249/
28
|
|
|
Introduction |
Introduction
In The life of an ASP.NET Request I looked at the pipeline that gets the request to the appropriate handlers. In the diagram I showed that further up the pipeline there were HTTPModules that allowed examination and modification of the HTTPApplication object as it got passed down. This article is going to take a more in-depth look at the HTTPModule and creation of one. |
The ASP.NET Page Flow and HTTPModules |
The ASP.NET Flow and HTTPModule
When the request gets sent to the
HTTPApplication object, it then gets sent to any HTTPModules that
are registered in the web.config file of the application.
There are already HTTPModules in
ASP.NET that are a part of every application. These include authentication,
authorization and session state modules and if you've ever handled the
Session_Start or Session_End events in global.asax then you have
used these modules (specifically the session state module).
Why use an HTTPModule
HTTPModules can take the request and
do anything to it before a Page object is created and used. For
example, if you only wanted people using Netscape to access your pages, then
you could write an HTTPModule that executed before a Page was
created to block other browsers. You could also use it to monitor performance
counters or calculate the elapsed time for a request.
What is an HTTPModule
An HTTPModule qualifies as anything
that implements the IHTTPModule interface. The interface only has two
methods -
Public Interface
IHttpModule
Public Sub Dispose()
Public Sub Init(context as HttpApplication)
End Interface |
|
Creating an HTTPModule |
Creation of an HTTPModule
Below is just a simple HTTPModule -
Imports System
Imports System.Web
Imports System.Diagnostics
Public
Class
SampleModule : Implements
IHttpModule
Public
Sub Dispose()
Implements
IHttpModule.Dispose
End
Sub
Public
Sub Init(ByVal
application As
HttpApplication) Implements
IHttpModule.Init
AddHandler application.BeginRequest,
AddressOf
Me.OnBeginRequest
End
Sub
Public
Sub
OnBeginRequest(ByVal
sender As
Object,
ByVal e
As
EventArgs)
Dim app As
HttpApplication = CType(sender,
HttpApplication)
Dim browscap
As HttpBrowserCapabilities
= app.Request.Browser
If browscap.Browser = "Netscape"
Then
'w00t
ElseIf
browscap.Browser = "IE"
Then
'EEK!
Else
'wha?
End
If
End
Sub
End
Class |
As you can see, this class implements all of
the required methods, it also adds a handler for one of it's events. The event
handling is usually the way that you time your events correctly as shoving it in
the Init method isn't the most effective way.
Currently it doesn't do anything but check the
browser and print the name of the browser to the screen.
The next thing you have to do is add it to the
web.config of the application like -
<httpModule>
<add
name="SampleModle"
type="HttpModHan.SampleModule,
HttpModHan"
/>
</httpModule> |
<add name="A Name" type="Class,
Assembly" />
It should look like -
When running.
|
Events in HTTPModules |
Events in HTTPModules
Now we get to add some functionality to our
module.
Imports System
Imports System.Web
Public
Class SampleModule :
Implements
IHttpModule
Public Event
bdet As
bdetEventHandler
Public
Delegate Sub
bdetEventHandler(ByVal
sender As
Object,
ByVal e
As
BrowserEventArgs)
Public
Sub Dispose()
Implements
IHttpModule.Dispose
End
Sub
Public
Sub Init(ByVal
application As
HttpApplication) Implements
IHttpModule.Init
AddHandler application.BeginRequest,
AddressOf
Me.OnBeginRequest
End
Sub
Public
Sub
OnBeginRequest(ByVal
sender As
Object,
ByVal e
As
EventArgs)
Dim app As
HttpApplication = CType(sender,
HttpApplication)
Dim browscap
As HttpBrowserCapabilities
= app.Request.Browser
If browscap.Browser = "Netscape"
Then
RaiseEvent bdet(Me,
New
BrowserEventArgs("Netscape"))
ElseIf browscap.Browser = "IE"
Then
RaiseEvent bdet(Me,
New
BrowserEventArgs("Internet Explorer"))
Else
RaiseEvent
bdet(Me,
New
BrowserEventArgs("another browser"))
End
If
End
Sub
End
Class
<Serializable()>
Public
Class
BrowserEventArgs : Inherits
EventArgs
Private bname
As
String
Public
Property BrowserName()
As
String
Get
Return bname
End
Get
Set(ByVal
Value As
String)
bname = Value
End
Set
End
Property
Public Sub
New(ByVal
BrowserID As
String)
BrowserName = BrowserID
End
Sub
End
Class |
I've added a public event to the code called
bdet and added a delegate for it's custom signature (using the
BrowserEventArgs instead of EventArgs, see the Related Articles
for more information on this) and then raising the event
with the appropriate browser name.
Then in web.config
<httpModule>
<add
name="SampMod"
type="HttpModHan.SampleModule,
HttpModHan"
/>
</httpModule> |
And finally in Global.asax -
Imports HttpModHan
Sub
SampMod_bdet(ByVal
sender As
Object,
ByVal e
As
BrowserEventArgs)
Response.Write("You are using " & e.BrowserName)
End
Sub |
And will look something like -
When running.
Summary
Well, this article should have taught you the
basics of setting up an HTTPModule and adding some events to it to make
it even more useful.
|
|
|
|
Product Spotlight
|
|