These days, I’m developing a new .NET website which is hosted by Windows Server 2008 R2 and IIS 7.5. For some purposes, I was using the “Intelligencia.UrlRewriter” module which was introduced by Scott Guthrie and everything is working fine on my Windows Server 2003 and IIS 6 home server. I also tested it on my development machine which has a Windows 7 Ultimate installed and everything was going well.
For some security reasons, my hosting company restricted using rewrite modules and they suggested me to use IIS 7 native URL Rewrite extension.
During the migrating process, I realized that the “~” operator is not working correct! Fortunately Ruslan Yakushev was explained about the “~” failure in URL Rewrite module:
You can use the ~ operator in ASP.NET Web server controls to reference the root of the application directory when you need to set a path. However, if URL rewriting changes the directory hierarchy of the requested URL, this can cause links that are specified with the ~ operator to be resolved incorrectly. For example, imagine that the Default.aspx page at the root of a Web application named app1 contains the following Image control:
<asp:Image runat="server" ImageUrl="~/Images/MyImage.gif" />
If URL rewriting changes the URL from http://localhost/app1/folder/file to http://localhost/app1/default.aspx, the links that are specified with the ~ operator will be resolved relative to the rewritten URL path, which would be relative to the /app1 folder. The following example shows the resulting HTML markup for the img element:
<img src="Images/MyImage.gif" ... >
Because the browser requested http://localhost/app1/folder/file, it will try to obtain the image from http://localhost/app1/folder/Images/MyImage.gif. This results in a 404 (File Not Found) error.
The URL Rewrite module for IIS 7.0 includes an update for ASP.NET that fixes this behavior. The update causes the ~ operator in Web server controls to be resolved relative to the originally requested URL. In the previous example, the HTML markup in the response will contain the correct URL path for the img element, as shown in the following markup:
<img src="../Images/MyImage.gif" ... >
The update for ASP.NET applies only to .NET Framework 3.5 SP1 and later. In order to get the update, upgrade the .NET Framework to version 3.5 SP1 and then run the IIS 7.0 URL Rewrite module installer, which will install the ASP.NET update.
But my problem didn’t solve! Even check my web server and saw that it has .NET Framework 3.5 SP1 and UrlRewiter 2.0 installed!!!
After lots of challenges, I realized, the “~” operator in my user controls that has the Output cache directive enabled, doesn’t work well and after I omitted the @OutputCache from my user control, everything was back to normal. Note that if you don’t use the UrlRewrite module in your application, everything will be working well, even if you set the OutputCache directive and I think it’s a bug that should be reported to the Microsoft.
I hope it helps.