As I mentioned in my last post, I've finally given urlrewriter.net a try - just to get over my php faithful friends who always sneered at us for those .aspx extensions.

I watched this video: How Do I: Implement URL Rewriting? by Scott Golightly. While it was good for the basic stuff (and informative), you'd have to code everything by hand. Also,near the end, he shows an implementation of a custom Form class that can handle postbacks with url rewriting. This has the major drawback of having to change the form tags on every page, and also having to register the control on every page....i.e. it's messy. The actual implementation also seems to totally remove the action on the form entirely. Of course, as asp.net forms submit to themselves (non-MVC), I don't know how big a problem this is.

Anyhow, I went on over to the Gu's tutorial - something I have been wanting to do for ever so long. I did everything according to method three. Everything worked as expected...except for images. If I created some redirect rules that traversed folders (i.e. www.site.com/folder/page rewrites to www.site.com/home.aspx?f=folder&p=page), sometimes the themes and images were loading, sometimes they weren't (depending on the rewrite rule). As ScottGu mentioned on his blog, making images have absolute paths helps; but themes seem to cause a problem with more complex rules.

So, I thought for a bit and added this little rewrite rule to the mix:

<rewrite url=".*/App_Themes/(.+)" to="~/App_Themes/$1" />

What this does is that it takes any request to anything with App_Themes in the url and maps it to the root App_Themes folder. So, no matter what the level of nesting, and no matter what rewrites are used, anything I the App_Themes folder will work perfectly. This works perfectly with themes. And as an added bonus, relative urls like "App_Themes/theme1/image.png" (as opposed to absolute urls like "~/App_Themes/theme1/image.png") will also work for images, css files, etc. And if you don't like the App_Themes folder, you can always add a rewrite for another folder using the same technique as above.

Needless to say, for this to work, you'll need to map every request to asp.net (just as you need with extensionless urls). This is damn easy in IIS7, and tougher on IIS6, as ScottGu points out.

 

So, there you have it, a simple hack that makes themes and resources work in perfect harmony with url rewriting. Can't wait to show my php religious friends. And just to add insult to injury, I made an asp.net page specifically for them:

http://Heartysoft/LookAtThisAspxPageYouMoron.php

Can't wait till they wake up and log onto the LAN ;)