Monday, January 19, 2009

Setup Http Handler For a Particular Folder in IIS 5 and 6

The other day I was setting up my custom ASP.Net http handler in IIS 7. Easy and painless. Using IIS manager just go to the folder you like and configure it, a snap. I tried to do the same thing in IIS 5.1 (the same thing with IIS 6), but no such luck. If you want to do it in a virtual folder, then that's not a problem - it's easy to set it up. However I wanted to set it up on a particular subfolder - no luck, IIS manager does not give you that option.
I was handling .js files so setting the handler on the top folder was a bit of an inconvenience. Luckily it is just a limitation of the IIS manager, not the IIS itself. What you need is to get yourself Metabase explorer which is a part of the IIS 6.0 Resource Toolkit and do a simple surgery.
Here is what I did:
I added folders beneath my virtual directory - Test in this case (Scripts and Dynamic), and added ScriptMaps key:

Normally it would inherit ScriptMaps from Test folder something like this:

I want it to handle only .js files so I added the key ScriptMaps (since it was inherited from the virtual directory it was not showing up in the list of keys):
And added a value for my files:
or in plain text that is:
.js,c:\windows\microsoft.net\framework\v2.0.50725\aspnet_isapi.dll,1
note the . (dot) at the beginning and the path to the ASP.Net isapi filter might be different on your machine.
And that's it, I am only handling files I want to be handled by me in this folder, I can setup other file types if I wish in future. You still need to register your http handler, slight variation to the explanation is to put Scripts\Dynamic\*.js (in this case) for path attribute value.
I will add post on how to set this programatically soon.

2 comments:

  1. I was trying similar trick where I wanted everything under a given folder (CONTENT) to be processed by my custom handler.


    Like, any request to http://localhost/MyHTTPHandler01/Content/India/UP/Mathura

    should be processed by my handler.

    So I went to IIS, and created a new application for Content folder. Then I configured my ContentRequestProcessorHandler.Dll to handle .* extension without checking that the file exists.

    Came back to IIS, removed the application that I created for my Content folder.

    Now IIS remembers the .* application extension that I defined for my Content folder and any request to anything under Content folder is served by my ContentRequestProcessorHandler.dll

    Of course, I made entries in my web.config as well.

    Like:

    <add verb="*" path="Content/*" type="ContentRequestProcessorHandler.ContentRequestHandler, ContentRequestProcessorHandler"/>

    ReplyDelete
  2. Hi Vishal,
    I've done that too, and you could also create a virtual folder only for that particular folder. However the IIS metabase explorer gives you more control, and creating virtual folders becomes a PITA after a while.

    There's a programmatic way to do it which comes handy if you need to automate the process, I'll blog soon about that.

    Cheers,
    Mihailo

    ReplyDelete