0

I feel like a nub for asking this, but I can't figure it out.. I've found several posts (here's one) saying that to use a relative path from the root of your site, start the path with /, ex:

<img src="/images/img1.jpg" />

My file hier. looks like

-root
  -images
  -css
  -index.aspx
  -subFolder
    -test.aspx

Now when I use the src path as shown above, it doesn't work in either index.aspx or test.aspx, but when I remove the /, it works for index.aspx. In test.aspx, I used ../images/img1.jpg and it works. What gives? Why is my example above not working?

Community
  • 1
  • 1
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119

2 Answers2

1

Your site is probably in a virtual directory, so the "/" refers to the actual web root as IIS (or whatever web server) sees it - maybe the folder above your 'root' folder

.

Ray
  • 21,485
  • 5
  • 48
  • 64
  • Thanks, yeah that was it. Why would it do this? The folder above that is `Websites` where all my VS2008 projects are. This means I'm going to have to change all my paths when I deploy to the server?! Do you know by any chance if there is a way to configure the 'root' folder in VS? – Nick Rolando Oct 31 '11 at 22:15
  • I don't run sites from VS, so I don't really know. But I don't think so. I use IIS 5 on XP, so there is only one root (or 'default') site. Anything else must be virtual - which sucks because that is not how it works in production with a 'real' web server. I keep a 'rootFolder' string in memory, which I prepend everywhere. The value is '/root' for dev, and just '/' for production. A pain, but it works. – Ray Oct 31 '11 at 22:20
  • Oh, I gotchya about the virtual directory now.. it doesn't actually go to the parent folder of the 'root'. I was able to change the root by selecting the website in solution explorer and changing `Virtual Path` to `/` in the properties panel :) Thanks again! – Nick Rolando Oct 31 '11 at 22:35
1

The relative paths work because they are traversing the directory based on the location, so for index.aspx it needs to go into images folder and get the img1.jpg, for test.aspx it needs to go up one level .. then into the images folder and get the img1.jpg.

Absolute paths are based off of were the application is installed from based on IIS settings. If you are just testing this from that folder your absolute path needs to include the root folder. /root/images/img1.jpg and then it will work from both test and index with the same absolute path.

benjamin
  • 1,087
  • 7
  • 10