UrlRewriter.net and Themes

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 ;)

11 Comments

  • Yes...heartysoft/LooakAtThisPageYouMoron.php is broken...I put that page up on my LAN to show my friends. Come to think of it, it'd be fun to actually host the page and put stuff like how asp.net is more powerful than php on it. Let the flame wars begin!!

  • Could you post an example of your rule for implementing the multiple queryString technique? I am strugling with it and would appreicate the help!!

    (i.e. www.site.com/folder/page rewrites to www.site.com/home.aspx?f=folder&p=page),

    Thanks.

  • That would be something like:

  • About many folders?

    www.site.com/folder1/folder2/folder3/folderN../../../page.aspx

    Thank you

  • With great vocals performed by Fay Aiyana from the end credits of HBO's Sin City Diaries and Jeff Costa's Cardio Striptease this album is a must-have!

  • Considering that, it is no surprise that a stripper pays close attention to stripper attire. Exotic dance apparel can come in very elaborate styles and

  • A new type of airport X-ray scanner that sees through all clothing and displays the private parts of fliers are being used or will be used at.

  • RapidShare File Search Results: 1-4 of 4 for party strip. Sort Results By: Size + - Alphabet + -. Sponsored Links, party strip.full.rar

  • What's your favorite section of the Sunday paper? If you're at all like me, you flip to the comics first.

  • Professional dancers from the Dancing with the Stars TV show: Maksim Chmerkovskiy and Cheryl Burke. GENRE: Fitness. AVAILABLE IN:

  • Hi
    How to write following url
    http://www.roommatetonic.com/utilisateurs_profil.aspx?u=783&membre=benjamin
    into following
    http://www.roommatetonic.com/us/directory/benjamin

    Where "us" is the current culture which should come from session variable & "directory" is fixed word.

    What should be the expression in web.config

    Thanks in advance


Comments have been disabled for this content.