Search code examples
asp.netvirtual-directoryiis-5

Virtual Directory root vs Default web site root


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


Solution

  • 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"/>