开发者

How to get root directory of Virtual Application or Virtual Dir in IIS7?

I try to use tag in html which run on client side only.

In my IIS7 has a virtual application call "testsite". I must call "localhost/testsite" to access this site.

assume that, I have 1开发者_开发技巧 image in "testsite/images" and I wanna get image from root path.

for example:

<img src="/image/image1.jpg" />

But the path of image that show in browser is "localhost/image" it should be "localhost/testsite/image". ?

What should I do? How to config an IIS7 to make them see a virtual application to be a root dir of that website?

Additional: Suppose that my HTML is in database and it has a lot of

<img src=/image/[imagename]>

And I display this text by adding it to Text property of control, so it cannot use an runat="server" in img tag.

What is the best solution to replace an src property of img tag in my database and display in it ASP.Net page.


You can try:

<img src="<%=Page.ResolveUrl("~")%>/image/image1.jpg" />

(assuming your using ASP.NET)


Use ~/ to point to the root of a web application. This will only work on serverside controls - so you will need to change your image tag to:

<img src="~/image/image.jpg" runat="server" />

Alternatively, simply output the value in the attribute:

<img src="<%:Page.ResolveUrl("~/")%>image/image.jpg" />

Update:

Since you are outputting within a literal control, you can simply use Page.ResolveUrl("~/") when setting the Text property of the control, similar to the alternative I have shown.


As i can see you are using HTML tag, a cleaner approach would be to use base tag in the head section of the page.Thus you will not be required to repeat server code in each html tag.

<base href="<%= Context.Items["baseURL"] %>" /> , i am reading this from web.config.

or <base href="<%=Page.ResolveUrl("~")%>" />

Now you can simple use <img src="/image/image1.jpg" />

I doubt Page.ResolveUrl("~/") will work when website is hosted inside a sub-directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜