[ACCEPTED]-IHttpHandler vs IHttpModule-httpmodule

Accepted answer
Score: 71

An ASP.NET HTTP handler is the process (frequently referred 18 to as the "endpoint") that runs 17 in response to a request made to an ASP.NET 16 Web application. The most common handler 15 is an ASP.NET page handler that processes 14 .aspx files. When users request an .aspx 13 file, the request is processed by the page 12 through the page handler. You can create 11 your own HTTP handlers that render custom 10 output to the browser.

Typical uses for custom 9 HTTP handlers include the following:

  • RSS feeds To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. You can then bind a file name extension such as .rss to the custom handler. When users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.
  • Image server If you want a Web application to serve images in a variety of sizes, you can write a custom handler to resize images and then send them to the user as the handler's response.

An HTTP module is 8 an assembly that is called on every request 7 that is made to your application. HTTP modules 6 are called as part of the ASP.NET request 5 pipeline and have access to life-cycle events 4 throughout the request. HTTP modules let 3 you examine incoming and outgoing requests 2 and take action based on the request.

Typical 1 uses for HTTP modules include the following:

  • Security Because you can examine incoming requests, an HTTP module can perform custom authentication or other security checks before the requested page, XML Web service, or handler is called. In Internet Information Services (IIS) 7.0 running in Integrated mode, you can extend forms authentication to all content types in an application.
  • Statistics and logging Because HTTP modules are called on every request, you can gather request statistics and log information in a centralized module, instead of in individual pages.
  • Custom headers or footers Because you can modify the outgoing response, you can insert content such as custom header information into every page or XML Web service response.

From: http://msdn.microsoft.com/en-us/library/bb398986.aspx

Score: 18

As stated here, HttpModules are simple classes 6 that can plug themselves into the request 5 processing pipeline, whereas HttpHandlers 4 differ from HttpModules not only because 3 of their positions in the request processing 2 pipeline, but also because they must be 1 mapped to a specific file extensions.

Score: 16

IHttpModule gives you much more control, you can basically 13 control all of the traffic directed to your 12 Web application. IHttpHandler gives you less control 11 (the traffic is filtered before it reaches your 10 handler), but if this is sufficient for 9 your needs, then I see no reason to use 8 the IHttpModule.

Anyway, it's probably best to have 7 your custom logic in a separate class, and 6 then just use this class from either IHttpModule or 5 IHttpHandler. This way you don't really have to worry 4 about choosing one or the other. In fact, you 3 could create an extra class which implements 2 both IHttpHandler and IHttpModule and then decide what to use by setting 1 it in Web.config.

Score: 6

15 seconds has a nice small tutorial giving practical 1 example

Score: 5

Modules are intended to handle events raised 25 by the application before and after the 24 request is actually processed by the handler. Handlers, on 23 the other hand, aren't given the opportunity 22 to subscribe to any application events and, instead, simply 21 get their ProcessRequest method invoked 20 in order to the "main" work of 19 processing a specific request.

Take a look 18 at this documentation from Microsoft (about 17 half way down the page in the "The 16 request is processed by the HttpApplication 15 pipeline" section):

http://msdn.microsoft.com/en-us/library/bb470252.aspx

You can see in step 14 15 where the handler gets its chance to 13 execute. All of the events before and after 12 that step are available for interception 11 by modules, but not handlers.

Depending on 10 what specific features you're trying to 9 achieve, you could use either a handler 8 or a module to implement an upload handler. You 7 might even end up using both.

Something to 6 consider might to use an upload handler 5 that's already written.

Here's a free and 4 open source one:

http://www.brettle.com/neatupload

Here's a commercial one:

http://krystalware.com/Products/SlickUpload/

If 3 you look at the documentation for NeatUpload, you'll 2 see that it requires you to configure a 1 module.

More Related questions