Extend ASP.NET with HTTP modules

by iatanasov 23. September 2007 04:04

Like in other .NET-applications, exception-handling is also available in ASP.NET-applications. There are a few extensions and special features for exception-handling in ASP.NET that will be explained in this article: Predefined IHttpModules allow you to process incoming requests and outgoing responses to and from an ASP.NET application. This article offers a closer look at using HTTP modules in ASP.NET applications.

Introduction to HTTP modules

ASP.NET HTTP modules provide access to incoming and outgoing traffic to a Web application. They are similar to Internet Server Application Programming Interface (ISAPI) filters in that they run for all requests, but HTTP modules are written in managed code and are fully integrated with the lifecycle of an ASP.NET application.

HTTP modules provide pluggable functionality to ASP.NET applications. These modules are added to the request pipeline before and after the ASP.NET HTTP handler fires. HTTP modules are different than HTTP handlers. HTTP modules are called for all requests and responses, whereas HTTP handlers run only in response to specific requests.

HTTP modules and the Global.asax file available in all ASP.NET applications provide the same functionality, but the implementation is a bit different. The Global.asax file requires code in the application, thus compilation when it changes; HTTP modules are completely separate.

A great aspect of HTTP modules is that they are built without affecting existing applications — albeit the extensible  architecture. These are easily added or removed to and from an ASP.NET application via the Web configuration (web.config) file.

Some common uses of HTTP modules include the following:

  • Headers: You can easily inject custom header information into each page.
  • Logging: You can easily gather logging or statistical data for every request. The creation of a custom HTTP module provides a central location for logging as opposed to code in every page.
  • Security: You can perform custom authentication or security checks on each page request. You may filter based upon user credentials, IP address, etc. The ASP.NET MemeberShip Provider work in this style.

How HTTP modules work

HTTP modules are registered in the web.config file. This ties the module to an application. As a result, when ASP.NET creates an instance of the HttpApplication class for an application, instances of any modules that have been registered are created as well. The module’s Init method is called when it is instantiated.

Within the Init method, the module subscribes to one or more events from the HttpApplication object. These events correspond to user actions within an ASP.NET application. These events include the following:

  • AcquireRequestState: This is the event you call to acquire or create the state for the request.
  • AuthenticateRequest: This event fires when a security module needs to authenticate the user before it processes the request.
  • AuthorizeRequest: This event is fired by a security module when the request needs to be authorized after authentication.
  • BeginRequest: This event signals that a new request is beginning.
  • Disposed: This event signals the application is ending, so it allows the module to perform clean up operations.
  • EndRequest: This event signals that the request is ending.
  • Error: This event is called when an error occurs during request processing.
  • PostRequestHandlerExecute: This event signals that the handler has finished processing the request.
  • PreRequestHandlerExecute: This event signals that the handler for the request is about to be called.
  • PreSendRequestContent: This event signals that content is about to be sent to the client.
  • PreSendRequestHeaders: This event signals that HTTP headers are about to be sent to the client.
  • ReleaseRequestState: This event signals that the handler has finished processing the request.
  • ResolveRequestCache: This event is triggered after authentication.
  • UpdateRequestCache: This event fires after a response from the handler.

An HTTP module may use any of these events.

Creating an HTTP module

You build a custom HTTP module using the IHttpModule interface, which you can find in the System.Web namespace. You want to possiblity to handle errors is in an own IHttpModule. To define it, you have to implement the interface IHttpModule and bind your error-handler in this class:

public void Init(HttpApplication app);
public void Dispose();
The Init method is important since it is called when the HTTP module is called to process incoming requests or outgoing responses. Calls to events (from the previous list) are placed in this method. If one of the events from the HttpApplication class is used, the code to handle the event must be included as well. This includes a delegate, the method, hooking to the event, and so forth.

Once a module is built, it may be registered by way of the httpModules section of the web.config file as the following snippet illustrates:

<system.web>
<httpModules>
<add type="[Class], [Assembly]" name="[ModuleName]" />
<remove type="[Class], [Assembly]" name="[ModuleName]" />
<clear />
</httpModules>

The best way to grasp the use of HTTP modules is to build one. HTTP modules are created as a class library. When using Visual Studio, a reference to the System.Web namespace is added to the project to access the required classes.

The following C# sample demonstrates a simple HTTP module that is tied to the BeginRequest and EndRequest events for a user request. The code displays a simple message in the browser stating the event fired. The events are registered in the module’s Init method with a method created for each event handled.

public class ErrorModule : IHttpModule

{

public ErrorModule()

{

}

#region IHttpModule Members

public void Dispose()

{

}

public void Init(HttpApplication context)

{

context.Error
+= new EventHandler(context_Error);

}

void context_Error(object sender, EventArgs e)

{

Exception e
= HttpContext.Current.Server.GetLastError();

// Clear the error so that it doesn't occur again in

// the Application_Error-handler

HttpContext.Current.Server.ClearError();

HttpContext.Current.Response.Redirect(
"ErrorPage.htm");

}

#endregion

}

 

So what is the benefit of using an own HttpModule. Well, the name already says it: You have a modularization for your error-handling. You can change your implementa

tion at one place without touching your application-logic and you can change the errorhandling of your appliation by defining another HttpModule and configuring it in your web.app..

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

asp.net 2.0

Related posts

Comments

October 21. 2009 15:35

Gravatar

Interesting ... as always - is your blog making any cash advance ? ;)

cash loans us

October 31. 2009 14:02

Gravatar

I always wanted to write in my site something like that but I guess you'r faster :)

easy personal loans us

December 17. 2009 00:15

Gravatar

Interesting stuff you've got here

payday loans us

December 23. 2009 04:52

Gravatar

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Free tutorial and ebook us

January 5. 2010 23:35

Gravatar

You made some good points there. I searched this topic and found out that most people will agree with your blog.

quick payday loans

January 28. 2010 14:46

Gravatar

To think creatively, we must be able to look afresh at what we normally take for granted.

Loans in UT us

February 12. 2010 08:51

Gravatar

Extend ASP.NET with HTTP modules. Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post..

herpes us

February 12. 2010 13:53

Gravatar

Extend ASP.NET with HTTP modules. Hello, maybe this is off topic but anyway, i've been browsing around your site and it looks really really neat. I'm building a new blog and struggling to make it look good, everytime i touch something i mess it up. How hard was it to build your site? Could someone like me with no experience do it, and add family update pages without wrecking it every time?.

herpes photo us

February 17. 2010 23:13

Gravatar

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful

teeth whitening reviews

February 18. 2010 23:55

Gravatar

Thinking will not overcome fear but action will.

teeth whitening free kit us

February 25. 2010 17:34

Gravatar

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

pay day loans

February 27. 2010 08:16

Gravatar

Very, very cool site site! I am loving it!! Will come back again - taking you feeds also, Thanks.

personal loans

February 28. 2010 12:11

Gravatar

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

Acne treatment

March 2. 2010 05:58

Gravatar

Extend ASP.NET with HTTP modules. Very interesting post I have seen here.Thanks for posting it..

herpes us

March 3. 2010 23:50

Gravatar

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Colon cleansers

March 4. 2010 13:47

Gravatar

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work. Big thanks for the useful info i found on Extend ASP.NET with HTTP modules.

cash payday loan

March 7. 2010 03:57

Gravatar

Extend ASP.NET with HTTP modules. It sounds like you're creating problems yourself by trying to solve this issue instead of looking at why their is a problem in the first place..

blood pressure us

March 7. 2010 14:31

Gravatar

Extend ASP.NET with HTTP modules. Very interesting post to hang on..Iam really impressed with this article..Looking for more info...

physical therapy us

March 8. 2010 20:52

Gravatar

Found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later .. Big thanks for the useful info i found on Extend ASP.NET with HTTP modules.

cash loans

March 13. 2010 16:24

Gravatar

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful

Stretch mark removal

March 26. 2010 11:41

Gravatar

I appreciate the article; it has given me http://www.folkd.com/user/jkljkl a brief idea on this topic. You have done a marvelous job by exploring this subject with such an honesty and depth. Thanks for sharing it http://www.folkd.com/user/heymate with us!

Altagracia Tomsche cn

March 26. 2010 12:01

Gravatar

I enjoying this site, this site some great resource that you http://delicious.com/ofself are providing and give it away for free. Thanks http://faves.com/users/thigpenmorgan for taking the time to discuss this site. I really loved reading this post.

Lou Cannone cn

March 26. 2010 12:27

Gravatar

Hey very nice blog!! Man .. Beautiful .. Amazing .. I will http://wordpress.org/extend/plugins/txt-as-post/ bookmark your blog and take the feeds also... Big thanks for the useful info i found.

Christine Himmelspach cn

March 26. 2010 13:06

Gravatar

I would like to http://www.extratasty.com/profile/23730/richard56 add your blog to my blogroll http://kaseyluvblog.blogspot.com/2010/03/drinking-water-filter-system-review.html please tell me what anchor should I use?

Soo Spirko cn

March 26. 2010 23:34

Gravatar

Hi Webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do! Big thanks for the useful info i found on Extend ASP.NET with HTTP modules.

toe nail fungus cure

March 30. 2010 08:04

Gravatar

Your blog provided us with valuable http://www.codeplex.com/site/users/view/richard56 information to help us get http://www.bigoven.com/~richard56 started.You have done an impressive http://www.naymz.com/richard_richard_3262174 job!

Jamel Talahytewa cn

March 31. 2010 01:50

Gravatar

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information. Big thanks for the useful info i found on Extend ASP.NET with HTTP modules.

cure for genital warts

March 31. 2010 11:06

Gravatar

The post is actually the freshest on this precious http://www.jumptags.com/r.sharp49/ topic. I concur with your http://www.mister-wong.com/user/rsharp49/ conclusions and will eagerly look forward to your future updates and saying thanks will not just be adequate, for the wonderful lucidity in your writing.

Toshiko Maruska cn

April 2. 2010 06:31

Gravatar

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

car loan with bad credit

April 3. 2010 13:06

Gravatar

That is some inspirational Never knew that opinions could be this http://www.blogigo.com/changemylife/need-say-somthing-about-Aquasana-drinking-water-filter/10/ varied. here.

Lesley Starke cn

April 5. 2010 08:30

Gravatar

Of course, what a great site and advisory posts, Can I add backlink - import your rss feed? Regards, Reader.

debt settlement programs

April 5. 2010 23:20

Gravatar

This is exactly what i was looking for. thank you for the informative post and keep up the good work! Big thanks for the useful info i found on Extend ASP.NET with HTTP modules.

new cellulite treatment us

April 6. 2010 15:06

Gravatar

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

recipe for leg of lamb cn

April 7. 2010 03:14

Gravatar

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment.

recipes for salad cn

April 8. 2010 21:53

Gravatar

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment.

drinking water filter system us

April 9. 2010 09:07

Gravatar

Shoot for the moon. Even if you miss, you'll land among the stars.

MD Payday Loans us

April 11. 2010 14:41

Gravatar

This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article!

low cost payday loans us

April 21. 2010 20:28

Gravatar

Hi Webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

Forum Software au

April 23. 2010 03:25

Gravatar

Extend ASP.NET with HTTP modules. Nice ... maybe you could update this. Thanks.

vivian us

April 24. 2010 08:01

Gravatar

Extend ASP.NET with HTTP modules. I usually don�t post in Blogs but your blog forced me to, amazing work.. beautiful �.

hanhan us

April 25. 2010 08:56

Gravatar

g

bike storage shed us

May 3. 2010 05:52

Gravatar

In searching for sites related to web hosting and specifically comparison hosting linux plan web, your site came up.You are a very smart person!

no fax cash loan us

Comments are closed

Powered by BlogEngine.NET 1.1.0.7
Theme by Mads Kristensen

About the author

Ivan Atanasov - web developer
E-mail me Send mail Subscribe Feed

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Pages

    Recent posts

    Recent comments

    Authors

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2012 it-coder.com

    Sign in