For the last days I had a lot of trouble doing my own URL Redirection with ASP.NET (3.5), MasterPages and "~" server side redirection for <img src>, <a href>, <link src=css> and other controls.
After a lot of work and searching, I finally found the solution.
on the root, I have all my aspx.
On the root/Masterpage folder, I have my .master files
And on the root/Usercontrols folder, I have all my ascx files.
my images are on the root/img folder.
First of all, before talking about redirection, let's talk about what .NET do with your "~" or ".." shortcut.
If these html tags are directly in a .master file (example <img src=../img/image.jpg>, then the ".." or "~" characters will be automatically managed by .NET even if the <img> control is not runat=server. Every html control directly on the .master file will be managed like runat=server objects, to be sure that all the references to other files will work fine, because your aspx files that use these master pages can be anywhere on your web site.
If these html tags are in .ascx files, in user controls, you must add the runat="server", id="something" to every tag you want a clean redirection (<img>, <a> or else). By doing it, the usercontrol will adapt all its containing html "runat=server" tags to the position of the containing ASPX file.
But, we did not do any url rewriting yet.
If I do url rewriting, let's say, I want to say "http://mysite.com/products/" to redirect to "http://mysite.com/productlist.aspx", the problems begins. When the "~" or ".." addresses are managed, they are transformed according to the root folder, because my productlist.aspx file is on the root. For example, if my masterpage have a <img src="../img/picture.jpg"> tag, it will be transformed in "<img src="img/picture.jpg">. The problem is that the URL shown in the browser is /products", so the browser is looking to find the file at this address: http://mysite.com/products/img/picture.jpg. It will never find it.
1 solution I tried is to use the <base> tag in the header, but I got some trouble with, and I have to write a <% %> tag in my .master file, and I hate it.
The best solution I found that seems to work really fine is this one: When you call the "Context.RewritePath" function, with your new url, use this syntax: Context.RewritePath(innerpath, False)
The False tag seems to make all page be calculated from the client url, not from the server url, and that solve every problems !!! It works fine with my page in the designer, in the debug mode with the integrated mini-iis, and with the real IIS too. That's fantastic! And, I'm happy to see that I found a solution that even Microsoft themself did not found! They recently release a urlrewriting utility and they say that they have a known bug, when resolving the "~" addresses ! (look at their "known issue" number 2!)
I tried the solution they told to use, with the Page_Preinit and the use of HTTP_X_ORIGINAL_URL server variable, but it was not working for me, I always have this variable set to nothing.
I hope it may help you. Have a good coding time!