问题
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?
回答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
.
回答2:
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.
来源:https://stackoverflow.com/questions/7960094/relative-path-from-site-root