IIS7 and Url Rewriting

IMPORTANT UPDATE I found out where the tag belongs! šŸ™‚

I was running in Classic .NETĀ AppPool, when switching to the DefaultAppPool in IIS7 I could use the section.Ā So the new way to use HttpModulesĀ for UrlRewriting is:

<modules runallmanagedmodulesforallrequests="true">
  <add name="RedirectEngine" type="RedirectEngine.Rewrite, RedirectEngine" precondition="managedHandler"></add>
</modules>
<validation validateintegratedmodeconfiguration="false"></validation>

No idea what the last tag does, but IIS7 placed it there, so I hope it does any good šŸ˜‰ ** **Continue with (old) articleā€¦ I have this website that needs to pump out some information to another application, just plain old xml. The problem is that this application was originally calling some weird app that was accessed through these kind of urls:

  • http://myserver/folder/giveme?id=120
  • http://myserver/folder/iwanttoknow?id=120&more=1

So my solution was to place a backslash, so youā€™d get http://myserver/folder/giveme/?id=120.That way, it would be redirected to default.aspx and handle the request. Unfortunately, not an option, as the url was created by the calling application, based on just the domain. So after /myserver/ I had no control over the url it looked at.

My alternative was url rewriting. When thereā€™s a request to some file, check the last part of the folder requested and redirect it to the right folder with a nice default.aspx in it. Until I ran into IIS7. **IIS7
**The problem here is, that the request to non existing files donā€™t pass my HttpModule anymore! I just get a 404 and no breakpoint inside my code gets hit. In IIS6 every incoming call is forwarded to my HttpModule. I kept on seeing requests for favicon.ico for example. Not in IIS7 though.

Donā€™t ask me what I tried to get it working. I looked at Scott Guthrieā€™s blog, but couldnā€™t find anything useful. Except this item, where Scott says youā€™ll have to configure IIS7 to make it work. Sure, thanks Scott, but how?! I also found this forum thread on iis.net but have no idea where the tag should go.

So whatā€™s my solution? I added a handler to IIS, which seems possible inside your web.config since IIS7.I added the following:

<system.webserver>
        <handlers>
            <add name="MyFiles" path="*" verb="GET" modules="IsapiModule" scriptprocessor="%windir%Microsoft.NETFrameworkv2.0.50727aspnet_isapi.dll" resourcetype="File"></add>
        </handlers>
    </system.webserver>

So now every unknown file extension will be redirected to the ASP.NET Isapi module. Meaning itā€™ll go through my HttpModule and I can redirect the request. This isnā€™t all though, it still wonā€™t transfer calls to me if thereā€™s no file. So for every request thatā€™s made, youā€™ll have to figure out what the file should be and place one in the folder. My files are, based on my demo urls in this article, ā€œgivemeā€ and ā€œiwanttoknowā€.

Remember, this is not a solution! It just plain sucks! I want it to work like in IIS6.However I need it to work right now on IIS7 as well, and fast, so I can finish this app. Iā€™d like to hear if people know how to get it working without the handler and empty files. So leave a comment or contact me if you know.