by
Robert C. Barth
First Posted February 16, 2009
With the release of .net 3.5 SP1 came the release of the URL routing framework, which makes it extremely easy to be able to map user-friendly URL's to different things inside your application (notably, this functionality is required for MVC).
This all works fine in IIS 7, but if you're using IIS 5.1 on Windows XP Pro (like me) or IIS 6.0 on Vista, you're hosed. It simply will not work without a bit of hackery. The prevalent hackery involves using so-called wildcard mapping to map all file extensions to the asp.net ISAPI dll. This probably works in IIS 6.0 (I don't have it, so I'm guessing), but it certainly does not work in IIS 5.1 (you'll get a bunch of happy-fun-time 404 errors, or, at least, I did, and so did at least one other person on the Internet that I saw). Besides, who wants every file request (including css, html, png, jpg, etc.) to go through the asp.net ISAPI dll? From what I understand, that can reduce the speed of a request by up to 30%. Yuck!
A much better way to do it is to do what I recently did:
- Set up a custom extension for your stuff. If you're using MVC, that extension is chosen for you (.mvc). For my purposes (a toned-down content management system), I chose ".content." Just go into the configuration for your application in the IIS Management Console and add your extension (as I mentioned, .content in my case).
- Modify your RoutingTable Route rule to look for a static file with your extension (in my case, I chose content.content, so paths would look like: "content.content/{stuff}" with "stuff" being the parameter data the routing infrastructure would turn into a key/value pair for me.
- Install your URL rewriting ISAPI filter. I used Ionic's ISAPI Rewrite Filter (IIRF). The thing you want to do here is rewrite URL's to your specific folder alluded to in step 2 (for me, /content/) to whatever you chose for your RoutingTable entry (in my case, content.content). So, what happens for me is, if a request for "/hostname/virtualfolder/content/stuff" is made, it rewrites the URL to "hostname/virtualfolder/content.content/stuff". This makes it so that the asp.net ISAPI dll will now handle the request (because of the .content "extension").
There, now you're done. Three simple steps. Depending on the URL rewriting ISAPI filter you choose, getting the rewriting to work might be a slight pain. For instance, IIRF uses regular expressions and I needed help figuring out the magic regular expression that would do the rewrite appropriately (I suck at creating regular expressions unless I spend a couple of weeks neck-deep in them, which is rare). Incidentally, RegExr is an awesome online Flash-based regular-expression testing tool and is what I use to test regular expression syntax. It helps because it not only shows you what will be captured in your sample text, it also shows you the capture groups and has all kinds of help describing what each character in your regular expression is doing.
I hope this saves someone some time.