What's the use of having HTTPModules ?

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.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
 
M

Matt Berther

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...
 
S

Scott Allen

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.
 
M

msnews.microsoft.com

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top