Base URL in ASP.net Master Pages with virtual Directories

前端 未结 5 672
既然无缘
既然无缘 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:

    <script src="<%= Server.HtmlEncode(GetAppRootUrl(false)) %>/scripts/ian.js" language="javascript" type="text/javascript"></script>
    
    0 讨论(0)
  • 2021-01-03 01:41

    So I found this IIS weirdness last night:

    <script src="/js/file.js"></script>
    

    Will not work properly in a virtual application that's in a subdirectory of the main IIS site.

    Instead, you MUST do it like this:

    <script src="/js/file.js" type="text/javascript"></script>
    

    Which is the standard way to do it, but if you're not looking for it, it can surprise you that that additional tag makes the relative path issues go away.

    0 讨论(0)
  • 2021-01-03 01:44

    Most tags, including regular HTML tags like <link>, <img>, etc can use the ~/ as the application root path if the *'runat="server"' attribute is set.

    eg.

    <img src="~/images/test.png" runat="server" />
    

    This makes tag a server tag and the tilde is replaced with the application root before the output is returned to the browser.

    This doesn't work as expected for the <script> though. When 'runat="server' is set for the script tag, then the script is considered to be server-side javascript and execution is attempted.

    To work around this you can either inject the javascript using one of the register client script methods. your you can use the <%= ResolveUrl('~')%> tag in your script tag.

    0 讨论(0)
  • 2021-01-03 01:47

    Use tilde(~) in yout reference (i.e ~/scrips/ian.js)...see if it works For links try Page.ResolveUrl in the .aspx page.

    0 讨论(0)
  • 2021-01-03 01:49

    All the urls are all declared as relative ie "/scripts/ian.js"

    Those seem to be absolute URL's that you're using, rather than relative URL's, which is probably why the <base /> tag isn't having the desired effect:

    This attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.

    -- from http://www.w3.org/TR/html401/struct/links.html#h-12.4

    You could try removing the leading '/' from your URL's to see if that works?

    Failing that, I tend to use ResolveClientUrl to get around issues like this, which you'd use in the same way as others have suggested using ResolveUrl:

    <script type="text/javascript" src="<%= ResolveClientUrl("~/path/to/js") %>"></script>
    ...
    <img src="<%= ResolveClientUrl("~/path/to/img") %>" alt="..." />
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题