Relative paths in IIS 7 Web applications under a website

≯℡__Kan透↙ 提交于 2019-12-05 19:27:33

Try this first

There is something about "Paths" for Asp.net beginners

UPDATE(as I'm struggling with this too):

Besides code behind MapPath("~/..."), my focus was only from HTML/CSS point of path resolution

I noticed if we talking about HTML

<img src="/images/x.jpg" />  this will work only from  

IIS 7.0
----MAIN WEB SITE

The slash "/" in front of "images" will be interpreted "go to root"

to fix for

IIS 7.0
----MAIN WEB SITE
    ----Web App #1

remove "/" so <img src="images/x.jpg" /> this will work for both, when hosted as site and when hosted as web application

Now CSS is tricky different way, I was surprised that using your example it doesn't work since there is no slash in front of the images.

consider a structure based on your expample

IIS 7.0
----MAIN WEB SITE:
    ----Web App #1
        ----styles
        ----images

Your CSS

background-image: url(images/someimage.png)

tells the browser to look for MAIN WEB SITE/Web App #1/styles/images/someimage.png

add "/" background-image: url(/images/someimage.png)

the result is MAIN WEB SITE/images/someimage.png

now add "../" background-image: url(../images/someimage.png)

the result is MAIN WEB SITE/Web App #1/images/someimage.png

(seems like resolved as go one level up, and then go to images)

The good part is, that once you change those prefixes to make it work as web application it seems to work even when hosted as actual web site if you need to do so

IIS 7.0
----MAIN WEB SITE (web app #1):
    ----styles
    ----images

[Sorry if this answer is not descriptive enough or messy, I will add edits when someone will propose or if I notice mistakes myself, generally firefox/chrome F12 and see paths for resources]

I add "runat="server"". Like this:

<a href="~/Default.aspx" runat="server">Home</a>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!