Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
What is the Difference between an httpModule and an httpHandler?

I get this question all the time and if you happen to attend one of my presentations on using each you should know by the end of the presentation. I also see this asked often on the ASP.NET forums and I thought it would be worthwhile to address the question here for a reference point.

The quick answer is httpModule works with the web application as sort of an adjunct to the request pipeline because modules generally add some processing in response to an event in the pipeline. An httpHandler actually processes the request type and renders the output being sent to the browser. A module can invoke a handler, not the other way around.

Custom modules implement the ihttpModule interface, which consist of a Dispose and Init method. In the Init method you would typically add event handlers to catch ASP.NET events, like AuthorizeRequest to perform custom logic. While you can access the Response filter to actually adjust the actual markup being sent to the client, this is very rare. Typically the module will process things outside of producing actual markup.

Custom handlers implement the ihttpHandler interface, which consist of the IsReusable property and the ProcessRequest method. The IsResuable property indicates to the ASP.NET engine if an instance of the handler can be reused for simultaneous requests. So typically this will return true for static content and false for dynamic content.

The ProcessRequest method operates much like the PageLoad event handler in the Page class (which by the way is an httpHandler). From the ProcessRequest method you initiate your code to actually build the markup being sent to the client.

I think these two distinctive aspects of ASP.NET get confused by many because they both serve near the metal. Both are responsible for separate tasks, but typically work in harmony to make ASP.NET the great framework it is.

Share this post :
Posted: Sunday, April 13, 2008 6:56 AM

by Chris Love

Comments

Eugene Katz said:

After attending your presentation at Code Camp 9 (thanks by the way for two engaging sessions), my final note on HttpHandles, when it finally clicked was "HttpHandles = [Java] servlets". Having come from a Java background, making that connection cleared things up.
# April 13, 2008 10:36 PM

James Devlin said:

Good clarification Chris, there's a lot of confusion out there about this topic. In fact, most people aren't aware that when it comes to URL rewriting, either an HTTP Module or HTTP Handler approach (not to mention using URL mapping via web.config) will work: http://www.codingthewheel.com/archives/7-ways-to-implement-url-rewriting As you mentioned in one of your other posts, a lot of people like to badmouth ASP.NET as not being "good for SEO" when ASP.NET is hands-down the single most powerful web development paradigm in the world, and can perform 100% clean URL rewriting/redirection. I think most comments to that effect are written by people who don't have a clue. :-)
# April 14, 2008 3:26 AM

al pascual said:

great to meet you at the conference
# April 15, 2008 12:28 AM

Kumanan said:

Hey Chris, I did attend your code camp session in Boston. It was great and I asked the same question to you. Your explanation is very clear and now I know it very very well. kumanan12@gmail.com
# April 17, 2008 9:57 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS