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 Article With Your Friends!