简体   繁体   English

虚拟目录根与默认网站根

[英]Virtual Directory root vs Default web site root

I am using iis 5.1 in which we have only only one default website, 我使用的是iis 5.1,其中我们只有一个默认网站,

I have two projects v2 and v3 我有两个项目v2和v3

my website points to v2 projects and have some folders images, styles etc now i have a virtual directory under this website that is hosting project v3 and having the same folder hierarchy as v2 我的网站指向v2项目,并且具有一些文件夹图像,样式等,现在我在该网站下有一个虚拟目录,该目录承载着v3项目,并且具有与v2相同的文件夹层次结构

in the home page of the both projects i have 在我拥有的两个项目的主页中

img src="\\images\\edlogo.gif" alt="logo"/> img src =“ \\ images \\ edlogo.gif” alt =“ logo” />

but this shows the same image that is in the v2 directory, How can i show different images for both projects. 但这显示的是v2目录中的同一图像,我如何为两个项目显示不同的图像。 using "\\" get the root of the web site but how can i get the root of virtual directory under that website 使用“ \\”获取网站的根目录,但是我如何获取该网站下的虚拟目录的根目录

This static method returns you full http path to root folder of your application (web site or virtual directory) 此静态方法将您的完整http路径返回到应用程序的根文件夹(网站或虚拟目录)

public static string GetAppRootUrl(bool endSlash) { 
   string host = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
   string appRootUrl = HttpContext.Current.Request.ApplicationPath;
   if (!appRootUrl.EndsWith("/")) //a virtual
   {
       appRootUrl += "/";
   }
   if (!endSlash)
   {
       appRootUrl = appRootUrl.Substring(0, appRootUrl.Length - 1);
   }
   return host + appRootUrl;
}

So, you can write in your page: 因此,您可以在页面中编写:

<img src="<%= Server.HtmlEncode(GetAppRootUrl(false)) %>/images/edlogo.gif" alt="logo"/>

Maybe you can use HttpRuntime.AppDomainAppVirtualPath or Request.ApplicationPath . 也许您可以使用HttpRuntime.AppDomainAppVirtualPathRequest.ApplicationPath

Too Page.ResolveUrl("~") is useful. Page.ResolveUrl("~")太有用了。

Usage sample for my virtual directory /v2 我的虚拟目录/ v2的用法示例

    HttpRuntime.AppDomainAppVirtualPath = /v2
    Request.ApplicationPath = /v2
    Request.FilePath = /v2/Inicio.aspx
    GetAppRootUrl(false) = http://localhost:2029/v2
    Page.ResolveUrl("~") = /v2/

there is something missing from your post, can you post it please? 您的帖子中缺少某些内容,您可以发表吗?

You could use relative pathing 您可以使用相对路径

 <img src="../images/edlogo.gif" alt="logo"/>

Your code sample will always get it from the root directory. 您的代码示例将始终从根目录获取它。

Use relative urls. 使用相对网址。 See here eg "images/bg.jpg" in the page "http://v2/default.html" will point to "http://v2/images/bg.jpg" while the same code in the page "http://v2/v3/default.html" will point to "http://v2/v3/images/bg.jpg" 参见此处,例如,“ http://v2/default.html”页面中的“ images / bg.jpg”将指向“ http://v2/images/bg.jpg”,而页面“ http: //v2/v3/default.html”将指向“ http://v2/v3/images/bg.jpg”

So your code becomes : 因此,您的代码变为:

img src="images\\edlogo.gif" alt="logo"/>

However, an unfortunate side effect is that you can't move your homepage around in your website directory structure without breaking the link. 但是,不幸的副作用是,您不能在不破坏链接的情况下在网站目录结构中移动主页。

I'm curious however why you would choose this sort of setup ? 我很好奇,但是为什么您会选择这种设置? Wouldn't it be easier to just make a v3 a website and place it on the same directory level as v2 ? 仅将v3设置为网站并将其放置在与v2相同的目录级别上会不会更容易?

Review System.Web.VirtualPathUtility Class & Methods (.Net 2.0 and Later) 查看System.Web.VirtualPathUtility类和方法(.Net 2.0及更高版本)

http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx

Review System.Web.HttpRequest Object 查看System.Web.HttpRequest对象

http://msdn.microsoft.com/en-us/library/system.web.httprequest.filepath(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/system.web.httprequest.filepath(v=vs.100).aspx

Public Function GetRoot() As String
    Return System.Web.VirtualPathUtility.MakeRelative(Request.FilePath, Request.ApplicationPath)
End Function

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

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