468,537 Members | 1,844 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

What's the use of having HTTPModules ?

Hi... I'm learning about HTTPModules, so please allow me a beginner's
question.

I read that "you gain low-level access to the HTTPRequests and responses
processed by the ASP.NET framework". But... I'm confused, because in this
example they use "AuthenticateRequest" and "EndRequest"... (they write code
for redirecting the user to a login page if the address is like
"YourSite.com/contents.aspx" instead of
"YourSite.com/contents.aspx?Username=Blah").

Imports System.Web
Public Class AuthenticationModule
Implements IHttpModule

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.OnEnter
AddHandler context.EndRequest, AddressOf Me.OnLeave
End Sub

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub

Public Sub OnEnter(ByVal s As Object, ByVal e As EventArgs)
End Sub

Public Sub OnLeave(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class

But my question is this: why do I need to write a HTTPModule at all ?
Really... I can write the exact same code in Global.asax, right ? Or is it
*NOT* the same ?
Thank you,
Alex
Nov 19 '05 #1
3 3052
Hello msnews.microsoft.com,

Yes, you can place this code in Global.asax and it will work just fine.

However, let me give a little different example:

In a recent project, we handled our authentication code in global.asax. However,
requirements changed (don't they always) and we ended up needing our authentication
code to be different per client.

So, we could do one of two things:

1) Bastardize the global.asax file with an if statement that handled this
specific client or
2) Factor the authentication out of the global.asax and create an AuthenticationModule.

Now, for the one-off client we were able to create a completely separate
assembly that included this HttpModule and then for this client, we switch
the web.config to use this separate HttpModule.

This is truly the object oriented way of accomplishing the requirement.

I hope this clears things up a bit...

--
Matt Berther
http://www.mattberther.com
Hi... I'm learning about HTTPModules, so please allow me a beginner's
question.

I read that "you gain low-level access to the HTTPRequests and
responses processed by the ASP.NET framework". But... I'm confused,
because in this example they use "AuthenticateRequest" and
"EndRequest"... (they write code for redirecting the user to a login
page if the address is like "YourSite.com/contents.aspx" instead of
"YourSite.com/contents.aspx?Username=Blah").

Imports System.Web
Public Class AuthenticationModule
Implements IHttpModule
Public Sub Init(ByVal context As System.Web.HttpApplication)
Implements
System.Web.IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.OnEnter
AddHandler context.EndRequest, AddressOf Me.OnLeave
End Sub
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
Public Sub OnEnter(ByVal s As Object, ByVal e As EventArgs)
End Sub
Public Sub OnLeave(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class
But my question is this: why do I need to write a HTTPModule at all ?
Really... I can write the exact same code in Global.asax, right ? Or
is it
*NOT* the same ?
Thank you,
Alex


Nov 19 '05 #2
Yes, you implement the same functionality from Global.asax, but,
HttpModules do offer additional flexibility. You can add and remove
modules from the processing pipeline just by modifying web.config, for
instance, and you can package your module in it's own assembly to use
across multiple projects if the need arises.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 1 Feb 2005 21:59:49 -0500, "msnews.microsoft.com"
<RE***********************@yahoo.com> wrote:
Hi... I'm learning about HTTPModules, so please allow me a beginner's
question.

I read that "you gain low-level access to the HTTPRequests and responses
processed by the ASP.NET framework". But... I'm confused, because in this
example they use "AuthenticateRequest" and "EndRequest"... (they write code
for redirecting the user to a login page if the address is like
"YourSite.com/contents.aspx" instead of
"YourSite.com/contents.aspx?Username=Blah").

Imports System.Web
Public Class AuthenticationModule
Implements IHttpModule

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.OnEnter
AddHandler context.EndRequest, AddressOf Me.OnLeave
End Sub

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub

Public Sub OnEnter(ByVal s As Object, ByVal e As EventArgs)
End Sub

Public Sub OnLeave(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class

But my question is this: why do I need to write a HTTPModule at all ?
Really... I can write the exact same code in Global.asax, right ? Or is it
*NOT* the same ?
Thank you,
Alex


Nov 19 '05 #3
Thank you, now it's much clearer - the purpose of doing something is almost
*never* explained in my book, just the technique :-(((

PS. Matt, see how lazy I am - I noticed that I have to change my account's
properties in outlook, but I haven't done it yet, therefore my alias is stil
msnews.microsoft.com :-))))
I'll change it right now !

Thank you both.
Alex.
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Aurel | last post: by
1 post views Thread by Craig Neuwirt | last post: by
reply views Thread by tshad | last post: by
1 post views Thread by Anonieko | last post: by
reply views Thread by andy6 via DotNetMonster.com | last post: by
2 posts views Thread by Mikael Syska | last post: by
3 posts views Thread by =?Utf-8?B?Tm9yZW1hYw==?= | last post: by
1 post views Thread by Samuel R. Neff | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.