Skip Navigation LinksHome > Categories > Code from a Category

How to Write a Custom HTTP Handler in ASP.NET



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 2010
Add Date: 11/15/2011
In recent years, there appears to be a merging between ASP.NET and Internet Information Services (IIS), Microsoft's Windows-based web server application. The result is that more and more server settings can be specified in your web application's web.config file.


I don't know about you but, not really being an "admin" guy, I'm really excited about this. It means I can make more customizations in my web.config file without having to delve into IIS. This is particularly cool if your site runs on a shared hosting account, where some IIS settings may not be available on a per-application basis. In addition, it also means that those settings can easily be transferred to another server should you need to move your site in the future.

If you are using ASP.NET 4.0 and IIS7 or later, implementing a custom HTTP handler is now much easier than it may have been in the past.

An HTTP handler is the routine that services a request for a particular group of files, or file types. For example, requests for ASPX files are handled through ASP.NET's Page HTTP handler, while graphics files such as JPG are not. If you wanted to implement special handling of request for JPG files, you can't do so from an ASPX file because ASP.NET code, by default, does not run when a request for these files are received.

However, you can create a handler for these files from ASP.NET if you create a custom HTTP handler. For example, what if you wanted to prevent other sites from linking to your graphics files? You could implement your own HTTP Handler that would see if the request came from tha same domain and, if not, deny the request. And thanks to improved integration between ASP and IIS, you can do it without tweaking IIS.

Creating a Custom HTTP Handler
Step1 shows my custom JpgHttpHandler. The class implements IHttpHandler and, as a result, must implement ProcessRequest() and IsReusable.

This is a very simple handler that starts by seeing if the request came from the current domain. If not, HTML markup for an "Access Denied" message is constructed and returned. Otherwise, the request file is written to the response stream. If an error occurs while writing the file, the code assumes the file does not exist and constructs and returns markup indicating that, while access was granted, the requested file was not found.

Step 1: Custom HTTP Handler

I should point out that, while this code is potentially very useful and is a great example of how an HTTP handler might be used, the technique shown is not foolproof. If you had to be absolutely certain that some content could not be accessed by some people, more sophisticated techniques would be needed.

Mapping the Handler
Now that we've written the HTTP handler, let's map it to JPG files so that this handler will be used for all requests for files with the JPG extension.

I actually ran into considerable trouble while first working this out. Initially, I added an section under . The trouble was that this worked just fine. Or, at least, it worked just fine on my development machine. Once I got everything how I wanted and uploaded it to my server, the site failed with a server error 500.

For this to work correctly on the server, an entry is needed under system.webServer. Interestingly, once I made this change, it did NOT work on my development machine. The server that comes with Visual Studio for debugging obviously does not behave exactly like IIS7. This can occasionally be a source of frustration and lost time. At any rate, the main concern here is getting it to work on the server and so I had to go with the system.webServer section!

Step 2 shows my entire web.config file. The system.webServer element was introducted with ASP.NET 4.0 and IIS7. Whereas this would have previously required making changes directly to IIS, this is all that's needed to implement our custom HTTP handler for JPG files.

Step 2: Web.config File


And that's really all there is to it. The attached website project has a default page with a link to a graphics file. Since the request will be from the same domain, the graphics file will be displayed. You can modify the handler's logic to have the "Access Denied" message displayed. Or you can enter the name of a non-existent JPG file to invoke the "file not found" message.

All customizations should be this easy! Hopefully, Microsoft will continue this integration of ASP.NET and IIS.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.