Base URL in ASP.net Master Pages with virtual Directories

前端 未结 5 671
既然无缘
既然无缘 2021-01-03 01:24

I have an ASP.net master page. In this master, I have all my css and javascript files defined. I also have a few images and a few buttons and hyperlinks.

All the url

5条回答
  •  孤城傲影
    2021-01-03 01:38

    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 master page:

    
    

提交回复
热议问题