Job Listing - Credit Cards - Mortgages - Fast Loans - Car Credit

PDA

View Full Version : truly relative pathz


crazidiamond
Jan 16th 2007, 6:44 am
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

frankcow
Jan 16th 2007, 6:48 am
I believe you can just use the '~' character, and it's automatically replaced by the app path

crazidiamond
Jan 16th 2007, 6:57 am
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 16th 2007, 8:20 am
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.

ccoonen
Jan 16th 2007, 12:01 pm
<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....

crazidiamond
Jan 18th 2007, 2:11 pm
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?

ccoonen
Jan 19th 2007, 11:04 pm
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.