locked
Problem using tilde (~) charachter with RewriteUrl RRS feed

  • Question

  • User1374858786 posted
    Hello, this is frustrating me substantially and I turn to the ASP.NET community for help (after searching the forums and Google A LOT!!).  I've been using the ~ charachter successfully for some time to resolve issues with designing on one URL (like localhost/myapp) and publishing to another (www.mydomain.com).  Although its totally a pain to use in the designer and not see your images appear (just trusting their their, and IMO violating the concept of separating code from design).  Usually I do something like this for my pictures:
    <img src="~/images/myimage.gif" runat="server" /> 

    This is great in that it at least works when deployed (although it is totally revolting when designing page layouts).

    I thought it worked by inserting <app path>/images/myimage.gif for the url, but it seems it actually takes this path and converts it to a URL relative to the page like ../images/mypage.gif or if I am 2 subdirectories deem from the root ../../images/mypage.gif again instead of localhost/myapp/images.gif, a path relative to the root.

    Where I run into trouble is combining this with mapping URLS to friendly URLS.  In other words, I'm using RegEx to convert a nice friendly url like
    http://www.socialnetwork.com/Venues/5/The_Tonic_Bar_Brighton.aspx
    into its true self:
    http://www.socialnetwork.com/Venues/VenueZoom.aspx?VID=5

    My regular expression parser takes the directory name after Venues/###/ and ignores everything else. I am using Regex + ReWrite in the Application_BeginRequest because I prefer this to writing a custom handler.  Anyway, here is where I get hosed and I think I encounter a bug.  Can you help me identify a workaround?

     The problem is I'm getting hosed by the ~ charachter!  On my page
    http://www.socialnetwork.com/Venues/VenueZoom.aspx?VID=5
    <img src="~/images/myimage.gif" runat="server" />  the url in the source property should get
    translated into ../../images/myimage.gif .  However, it instead is getting confused and it instead translates my url into this:  ../images/myimage.gif .  The problem is that because ASP.NET gave me this nice URL relative to the CURRENT PAGE using the .. charachter instead of an absolute path relative to the ROOT, my webbrowser can't find the picture, because to the web broswer ../images translates to:
    http://www.socialnetwork.com/Venues/images .

    In other words, the web browser thinks that I'm one level deeper into my website than the underlying page really is. 

    I tried inserting a <base> tag in the header but the results of this were a disaster.

    Is it possible to have ASP.NET convert the ~ charachter into a URL that is relative to the ROOT instead of relative to the current URL? 

    Does anyone know a workaround or a way to override the behaviour of how the tilde (~) is processed by ASP.NET 1.1?  I don't have Visual Studio 2005 yet (I ordered it though and I'd like to resolve this before I try to upgrade and try a VirtualPathProvider).

    Thank you so much for any help!

    Wednesday, January 11, 2006 9:25 PM

Answers

  • User-1372641848 posted

    I use following approch for <img / > tag.

    I have function that returns application path, function work both with IIS rool level application and IIS virtual application.

    public static string ApplicationPath{
                get { return HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + '/'; }

    }    

    and then all image tags and anchor tags are written as following.

    <img src="<%=ApplicationPath%>images/myimage.jpg" />

    <a href="<%=ApplicationPath%>home.htm">Home</a>          

    function works nicely when you are developing application using virtual path and then host at root level, if you develop and host in same way then you can use Request.ApplicaitonPath directly. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 13, 2006 10:04 PM

All replies

  • User-1029435529 posted

    I can help you with this. First, can you let me know whether you'd like the code in C# or VB.NET?
     

    Wednesday, January 11, 2006 9:40 PM
  • User-1029435529 posted
    Sorry, another question that I should have asked: are you doing the rewriting in Global.asax, in a codebehind file for Global.asax (which would inherit from System.Web.HttpApplication), or in an HttpModule?
     
    Wednesday, January 11, 2006 9:52 PM
  • User-1372641848 posted

    I tried inserting a <base> tag in the header but the results of this were a disaster.

    I do not know why base class does not work for you, I have used it in so sucessfully in so many applications. one such sample www.psychsource.com 

     

    Thursday, January 12, 2006 10:46 AM
  • User1374858786 posted
    I am writing the code using C# with code behind files.  I'm performing the URL rewrite in the Global.asax.cs file in the Application_BeginRequest event.

    Since my prior post I was able to figure out what I was doing wrong with BASE (I had the tag terminated which is wrong according to W3C and also I didn't specify a complete path to a page) but that didn't really solve any problems.  The BASE tag seemed to impact different types of controls differently.  For example, my ASP.NET hyperlink controls would work fine when using the ~ charachter but <img src="~..."  would not work.  What is happening now is I am ending up with complex rules about when I should use the ~ charachter and when I shouldn't.  Also some controls will work correctly using ~ + base in one folder but not in another folder. 

    I'm not sure why but when I'm using <img src="~/images/myimage.gif" runat="server"> if I'm in www.mysite.com/foldera/folderb/mypage.aspx the URL will render to the page as:
    <img src="../../images/myimage.gif" runat="server"> instead of <img src="/images/myimage.gif" runat="server">

    Herein lies my problem.  The page that is really running is in mysite.com/folderA but the webbrowser thinks its in mysite.com/folderA/folderB.  So asp.net converts my <img src="~/images/myimage.gif" runat="server"> into <img src="../images/myimage.gif" runat="server"> instead of <img src="/images/myimage.gif" runat="server">, and therefor my webbrowser ends up looking for the image in mysite.com/foldera/images/myimage.gif instead of mysite.com/images/myimage.gif.



    Friday, January 13, 2006 7:25 AM
  • User-1372641848 posted

    I use following approch for <img / > tag.

    I have function that returns application path, function work both with IIS rool level application and IIS virtual application.

    public static string ApplicationPath{
                get { return HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + '/'; }

    }    

    and then all image tags and anchor tags are written as following.

    <img src="<%=ApplicationPath%>images/myimage.jpg" />

    <a href="<%=ApplicationPath%>home.htm">Home</a>          

    function works nicely when you are developing application using virtual path and then host at root level, if you develop and host in same way then you can use Request.ApplicaitonPath directly. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 13, 2006 10:04 PM
  • User1374858786 posted
    Thank you Jigar this looks like a good idea.  I was hoping to have my tags utilize the minimum amount of code so that I have the best separation possible between code and layout/art.  However, I can only do so much.  I have to weigh the trade off of code separation vs. convenience and seo benefits of the friendly urls.  Thank you for your suggestion I think this is what I will go with.

    David


    Tuesday, January 17, 2006 9:11 AM
  • User-158764254 posted
    You should also be aware the calling ResolveUrl("~/someUrl") from codebehind will NOT result in the double-dot notation...
    Tuesday, January 17, 2006 1:06 PM