Skip to main content

Blog de Alejandro Avila

Go Search
Home
  

Blog de Alejandro Avila > Posts > Configuring Custom Http Handler in IIS 7
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.

ManagedHandler.jpg

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.

After installing , I attempted to load the web dashboard and got the following error display.

image

It said: "An ASP.NET setting has been detected that does not apply in the Integrated managed pipeline mode"

How confusing!! A quick look on the IIS7 documentation, blogs, forums etc I ran the following command.

%SystemRoot%\system32\inetsrv\appcmd migrate config "Default Web Site/".

I then refreshed the webpage and everything worked perfectly!

 

OR

 

Adding this to the webconfig in your application

<system.webServer>

<handlers>

<add name="*.jpg_GET,HEAD" path="*.jpg" verb="GET,HEAD" type="ThumbGen.ImageHandler,ThumbGen" preCondition="integratedMode,runtimeVersionv2.0" />

<add name="*.gif_GET,HEAD" path="*.gif" verb="GET,HEAD" type="ThumbGen.ImageHandler,ThumbGen" preCondition="integratedMode,runtimeVersionv2.0" />

<add name="*.png_GET,HEAD" path="*.png" verb="GET,HEAD" type="ThumbGen.ImageHandler,ThumbGen" preCondition="integratedMode,runtimeVersionv2.0" />

<add name="GifThumbGenerator" path="*.gif" verb="*" type="ThumbGen.ImageHandler" resourceType="Unspecified" preCondition="integratedMode" />

<add name="PngThumbGenerator" path="*.png" verb="*" type="ThumbGen.ImageHandler" resourceType="Unspecified" preCondition="integratedMode" />

<add name="JpgThumbGenerator" path="*.jpg" verb="*" type="ThumbGen.ImageHandler" resourceType="Unspecified" preCondition="integratedMode" />

</handlers>

<validation validateIntegratedModeConfiguration="false" />

</system.webServer>

Comments

There are no comments yet for this post.