简体   繁体   English

如何在IIS7中获取虚拟应用程序或Virtual Dir的根目录?

[英]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. 我尝试在仅在客户端运行的html中使用标记。

In my IIS7 has a virtual application call "testsite". 在我的IIS7中,有一个虚拟应用程序称为“ testsite”。 I must call "localhost/testsite" to access this site. 我必须呼叫“ localhost / testsite”才能访问该站点。

assume that, I have 1 image in "testsite/images" and I wanna get image from root path. 假设我在“ testsite / images”中有1张图片,我想从根路径获取图片。

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". 但是浏览器中显示的图像路径为“ localhost / image”,应为“ 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? 如何配置IIS7以使他们将虚拟应用程序视为该网站的根目录?

Additional: Suppose that my HTML is in database and it has a lot of 另外:假设我的HTML位于数据库中,并且其中有很多

<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. 我通过将其添加到控件的Text属性中来显示此文本,因此它不能在img标签中使用runat =“ server”。

What is the best solution to replace an src property of img tag in my database and display in it ASP.Net page. 替换我数据库中img标记的src属性并显示在ASP.Net页中的最佳解决方案是什么。

You can try: 你可以试试:

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

(assuming your using ASP.NET) (假设您使用的是ASP.NET)

Use ~/ to point to the root of a web application. 使用~/指向Web应用程序的根目录。 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. 由于您是在文字控件中输出的,因此在设置控件的Text属性时,可以简单地使用Page.ResolveUrl("~/") ,类似于我所展示的替代方法。

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. 如我所见,您正在使用HTML标记,一种更清洁的方法是在页面的头部使用基本标记,因此您无需在每个html标记中重复服务器代码。

<base href="<%= Context.Items["baseURL"] %>" /> , i am reading this from web.config. <base href="<%= Context.Items["baseURL"] %>" /> ,我正在从web.config中读取此内容。

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

Now you can simple use <img src="/image/image1.jpg" /> 现在,您可以简单地使用<img src="/image/image1.jpg" />

I doubt Page.ResolveUrl("~/") will work when website is hosted inside a sub-directory. 我怀疑将网站托管在子目录中时, Page.ResolveUrl("~/")是否可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM