Virtual Directory root vs Default web site root

6.3k views Asked by At

I am using iis 5.1 in which we have only only one default website,

I have two projects v2 and 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

in the home page of the both projects i have

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. using "\" get the root of the web site but how can i get the root of virtual directory under that website

5

There are 5 answers

2
meir On BEST ANSWER

This static method returns you full http path to root folder of your application (web site or virtual directory)

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"/>
0
Naveed Ahmad On

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.

3
Sem Vanmeenen On

Use relative urls. See here e.g. "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"

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 ?

0
Kiquenet On

Maybe you can use HttpRuntime.AppDomainAppVirtualPath or Request.ApplicationPath.

Too Page.ResolveUrl("~") is useful.

Usage sample for my virtual directory /v2

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

Review System.Web.VirtualPathUtility Class & Methods (.Net 2.0 and Later)

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

Review System.Web.HttpRequest Object

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