Tony Testa posted on September 19, 2007 00:00
What is an HttpHandler?
It is the earliest point where you have access to a request that is made to IIS.  In a typical application, a user calls .aspx pages, which maps to an HttpHandler, that handler is the PageHandlerFactory.  An HttpHandler is just like an aspx page except it doesn't have all of the UI elements therefore it doesn't have to process them.  HttpHandlers are therefore more reusable.  

Why would you want to use an HttpHandler?
Some great examples of why you would want to use an HttpHandler is in the case when you want to return an image from the database.  You could create an HttpHandler to call the database and return the Image.  Typically I used to create a page for this and override the response type to return the image.  With an HttpHandler, you can skip all the extra page processing and just return the image.
Another great example is when you want to download a file, such as a an excel file.  You could pass in a datatable, convert it, and then return it as an excel file.  Again, I used to create a page for this and override the response content types to do this, but an HttpHandler is a far better solution to the problem.

Real-World Examples
A real world example is on the blog engine I am using to write this post, SubText.  It uses a custom HttpHandler to map URL's to controls.
Another GREAT example is the Microsoft AJAX framework.  If you look at the base web.config file they give you.  See below for an extract of the <httpHandlers> section :

   34         <httpHandlers>
   35             <remove verb="*" path="*.asmx"/>
   36             <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
   37             <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
   38             <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
   39         </httpHandlers>

As you can see, a few custom file extensions are mapped to HttpHandlers.  This is how the Microsoft AJAX framework does most of the heavy lifting for you.  The HttpHandlers that are included handle inserting the necessary scripts into the page to perform the AJAX requests.

Demo
I made up a demo solution to show the basics of creating a HttpHandler.  Whats in the demo is 3 examples.  The first example is just a basic HttpHandler.  What it does when called is just return some basic text to the browser.  The second example shows creating a simple datatable and exporting it as tab delimited excel file.  The third example shows a more useful example where we call a database and export the results as a tab delimited excel file.

The real key to the HttpHandlers, besides implementing the interface, is adding the directives in the web.config file.

    1 <?xml version="1.0"?>
    2 <configuration>
    3     <appSettings />
    4     <connectionStrings/>
    5     <system.web>
    6         <compilation debug="true" />
    7         <authentication mode="Windows" />
    8 
    9         <!-- add this under the <system.web> -->
   10         <httpHandlers>
   11             <!-- you could specify a wildcard with a extension, or a specific name of a "page" to call -->
   12             <!-- you can also create an extension of your choosing, and tell IIS to process your extension as a handler -->
   13             <!-- <add verb="*" path="*.ashx" type="BasicHelloWorldHandler"/> -->
   14             <add verb="*" path="BasicExport.ashx" type="ExportToExcelHandler"/>
   15             <add verb="*" path="ExportDB.ashx" type="ExportToExcelDBHandler"/>
   16         </httpHandlers>
   17     </system.web>
   18 </configuration>

The above web.config file has commented out code which shows that we can map any file with the extension .ashx .  The other two <add> elements in the <httpHandlers> section shows how we can map a specific filename to the HttpHandlers we wrote.  As you can see, you pass in to parts, the "Path" which is the filename to map, and the "Type" which is the class to map the call to the filename to.

Download Files
You can download the demo solution here.
Its a website project, so create a directory for it, and extract the files there.  Then create a virtual directory for it in IIS or just run it in VisualStudio2005.

Posted in:   Tags: ,

Comments


Switzerland CmJosue
June 9. 2008 08:34
CmJosue
I have a question: how to call these handlers from the page, in case you want to execute ExportToExcel from with a button in the page.
Thanks, Joshua

no site


March 23. 2009 17:54
Slim Fit Blog
Just wanna say thank you for sharing this wonderful tips! It will really helps to broad my mind. Thanks for this useful information webmaster!

http://www.slimfitblog.com/http://www.slimfitblog.com/


March 23. 2009 18:01
home of natures
Hi, a pleasant day to all, I am enjoy reading your website or blog. I got many ideas for my blog. Thanks for the information about this topic.

http://www.homeofnatures.com/http://www.homeofnatures.com/


March 25. 2009 17:17
Put Up Blog
I am just hoping that you will continue writing the same great articles like this. This theme really get my attention. If you did it so, I will one of your loyal readers if ever. Thanks for this useful information.

http://www.putupblog.com/http://www.putupblog.com/


March 25. 2009 17:48
Put Up Blog
I was really looking forward to this. I really salute you webmaster for having this wonderful theme and for sharing your ideas. Hop that you will continue writing more articles than this. Have a great day. Thanks.

http://www.putupblog.com/http://www.putupblog.com/

Search Blog

Blog Roll

    OPMLDownload OPML file

    Recent Comments

    Banners

    Theme Grabber
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2012 Tony Testa's World