Abstract
HttpHandlers are powerful tools of ASP.NET; they are used fairly often, and give great power to developers using them. In this article, Brendan describes the basics of how to use these tools. He describes in easy to understand terms how one can implement the IHttpHandler class, and illustrates his explanation using code snippets and screen shots.
Introduction
One of the more interesting pieces of ASP.NET is the concept of HttpHandlers. Sadly, this article does not delve much into what handlers are or for what they are used. Instead, this article just gives a quick introduction for you to be able to write your own HttpHandler. The text following explains how to create a simple HttpHandler, and a few of the design decisions you will have to make when creating your own. You can download the sample project for this article here or at the bottom of the article.
There are plenty of ways in which one can benefit from the use of HttpHandlers. They are very versatile. They can be applied to a lot of different tasks. Keep in mind there are also HttpModules which are also extremely useful tools, but they are a bit different from HttpHandlers. I will not be delving into that topic in this article. Please read elsewhere to learn more about the differences between HttpHandlers and HttpModules.
I know plenty of people who have configured IIS to send all requests for gifs, pngs, jpgs, etc. to ASP.NET HttpHandlers so that site owners can clamp down on hotlinking, leeching, or whatever you want to call it. It is where someone else links to your sites content. It uses your bandwidth and you do not even get the traffic, and usually it does not mention where the content came from.
Other people will configure IIS to handle zip files so that an HttpHandler can step in and execute code every time someone wants to download a file from a site. This is much easier than some of the scripted downloads, and certainly gives great server-side control over the download - counting the number of downloads and storing in a database or whatever you want to do.
One other use of HttpHandlers, which I plan to write about soon, is to use them to watermark your images. If you have configured an HttpHandler to prevent people from leeching your images you might also use a handler to watermark your images with a copyright. This makes it less likely that people will simply save your images and display them on their own pages. Plenty of people and companies are quite concerned about this. Keep in mind that if you publish something on the internet it can never be completely safe, but you can at least discourage people.