Search code examples
.nethttphandlerhttpmodule

What is an HttpHandler used for and how is it different from an HttpModule?


I'm trying to understand what an HttpHandler is and what a HttpModule is and when I know that I must use one or the other of them. Please can you shed some light on this for me.

Thanks,

Sachin


Solution

  • Handlers are for responding to specific types of requests (e.g. you might write one for .png files to serve dynamic images)

    Modules are for manipulating the pipeline for potentially any type of request (e.g. you might use one to add, geo data to the HttpContext based on the request IP so that the code doesn't need to. Although in practise they tend to be more complex than that)

    That said, with the advent of technologies such as Asp.Net MVC it's possible to implement both sets of functionality as first-class features of your website's code.

    Cross-cutting concerns such as authentication etc (typically implemented with modules before) can now be done with Action Filters in MVC. And handling dynamic file types can be done with some clever routing. I've done the latter, for example, to implement dynamic content overriding of css/js and images using a database back-end.