truly relative pathz

Discussion in 'C#' started by crazidiamond, Jan 16, 2007.

  1. #1
    I would like to configure a web project with truly relative file and image pathz. regardless of server name or application name. I used to use"../dir_name/file_name" however an organizational shift forced me to move my entire directory structure down one level. instead of modifying "../dir_name/file_name" to "../../dir_name/file_name" i was attempting to use something like...

    <a href="<%=Request.ApplicationPath%>/Public/Home.aspx">

    and

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

    which works great. however when i try to use...

    <tr style="BACKGROUND-IMAGE: url(<%Request.ApplicationPath%>/Images/image_file.gif)

    I get zero background. also if I try...

    ImageUrl="<%=Request.ApplicationPath%>/Images/image_file.gif">

    I again get nothing but if I set ImageUrl in the c# code behind using a variation of the line above it workz. Can anyone explain either which part of my syntax is wrong or why you can use this function sometimes but not other times.
    Thanks,
    D
     
    crazidiamond, Jan 16, 2007 IP
  2. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #2
    I believe you can just use the '~' character, and it's automatically replaced by the app path
     
    frankcow, Jan 16, 2007 IP
  3. crazidiamond

    crazidiamond Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that did fix my imageurl property. however it doesn't seem to work correctly with this line...

    <tr style="BACKGROUND-IMAGE: url(~/Images/image_file.gif)

    any clue?
     
    crazidiamond, Jan 16, 2007 IP
  4. crazidiamond

    crazidiamond Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I ended up using this...

    BackGround.Style.Add("background-image", "url(" + Request.ApplicationPath + "/Images/image.gif)");

    why I cannot use Request.ApplicationPath with url() in the html i do not know.
     
    crazidiamond, Jan 16, 2007 IP
  5. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #5
    <tr style="BACKGROUND-IMAGE: url(~/Images/image_file.gif) - Make sure tr has a runat="server" for the ~ to work as app path. Other options that I have done is store a variable in web.config as your Web Path Root then use that everywhere <% configurationmanager.app....
     
    ccoonen, Jan 16, 2007 IP
  6. crazidiamond

    crazidiamond Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    could you elaborate on the webconfig file? i thought about using this but i had trouble accessing the variablein the html.


    with what i am trying to do this workz, however i would much rather stay in the html than go to the c#

    BackGround.Style.Add("background-image", "url(" + Request.ApplicationPath + "/Images/image.gif)");

    When i tried this line in the html...

    <tr id="BreadTrail" style="BACKGROUND-IMAGE: url(~/images/BreadTrail.gif)" runat="server" height="20" width="100%">

    i get this in the html source but it did not translate the tilde...

    <tr id="BreadTrail" style="BACKGROUND-IMAGE: url(~/images/BreadTrail.gif)" height="20" width="100%">

    any idear?
     
    crazidiamond, Jan 18, 2007 IP
  7. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #7
    ohh man, you should always be using Web.Config for anything that might be a "magic number". A company phone number, a contact email, a global variable, connectionstring, anything. Always use Web.Config, and when going Live - you change one file and your good to go :)

    <appSettings>
    <add key="AllowComments" value="false" />
    </appSettings>

    Now anywhere in your site, you can do:
    Dim AllowComments as Boolean = Boolean.Parse(ConfigurationSettings.AppSettings("AllowComments"))

    (it's like a free hook for any global variable you want)

    But, I setup a Variable called WebRootLocation and give it a value like "/testing/fun/" - this way it sees http://localhost/testing/fun/ as my webroot - and when you go live, you change this to "" and it sees http://localhost/ as the webroot.
     
    ccoonen, Jan 19, 2007 IP