Configuring Custom Http Handler in IIS 7
Recently when working on an application, I was playing around with writing custom http handlers. There is nothing new in that lots has been written about how to build and configure custom http handlers. So when after writing this I decided to configure it in IIS 7 on my Vista machine, I was pleasently surprised to see that I was lost !
IIS 7 UI has changed noticeably from its earlier versions and when I wanted to configure the file extension I wanted my custom handler to work with, I didn't know what to do.
Fortunately, I found that when viewing my particular website in IIS 7, in the two groups that IIS displayed the information (Grouped by Area by default and has ASP.NET and IIS groups), there was an option called Handler Mappings in IIS group. This seemed like the most relevent location where to look further.
Opening the Handler Mappings, the listing for all file extensions registered with various handlers was displayed, so this was the right place. However as I looked for an "Add new file mapping" option, I instead found three options
1. Add Managed Handler...
2. Add Script Map...
3. Add Module Mapping...
Nothing what each of these meant, I clickly opened each and figured out that managed handler had something to do with handlers witten in managed code, script map was to help configuring an executable and module mapping to work with http Modules. For more details do refer to the IIS 7.0 help file installed with IIS or online on technet.
I soon realized that Add Managed Handler is what I need to work with. There is a catch about Application pool setting being Integrated or ISAPI, which again you can get more details about in the IIS 7 help files. I did notice that on my Vista RTM, the two modes were Integrated and Classic. Since I didn't work with pre RTM versions, I can only guess that Classic was earlier called as ISAPI.
OK, so back to adding managed handlers. See the figure below to see how this looks like.

The interesting point is that after adding a custom http handler (you can follow MSDN documentation's How To article for this) and building the site, IIS already seemed to understand that I had a custom Http handler in my application and it displayed it in the Type drop down (HelloWorldHandler in the figure above). I selected it, provided *.sample as the extension I wanted to work with and also gave it a friendly name.
Completing the above steps also automatically edited the web.config file to add the necessary information about the custom handler. However unlike earlier where we added <httpHandlers> within <configuration><system.web> node, I got a new entry in the web.config inside of <configuration> node as below
<system.webServer>
<handlers>
<add name="Custom Handler" path="*.sample" verb="*" type="HelloWorldHandler"
resourceType="Unspecified" />
</handlers>
</system.webServer>
Finally to test that it all worked, I accessed the handler as http://localhost/MySampleSite/Test.sample and I did see the message displayed on the page as written on the httpHandler code.
Hence when working with IIS 7, one needs to write the implementation class for the http handler and build the project. Configuring in IIS and also making the necessary entries in Web.config is easily taken care via the Add Managed Handler option in IIS 7.

Comments
How can i make that custom extension to be handled by aspnet_isapi.dll? So, for example a file named test.sample should be treated as if it were test.aspx?? I have been playing around on IIS 7 on Windows Vista Home Premium but no luck.
Posted by: Rafay Bin Ali | April 12, 2008 01:45 AM
Rafay, you will find some information here - http://blogs.iis.net/thomad/archive/2006/11/04/precondition-what.aspx
Posted by: Atul Gupta | April 15, 2008 04:18 AM