~/ equivalent in javascript

后端 未结 12 1935
萌比男神i
萌比男神i 2020-12-12 19:11

Any smart way of doing a \"root\" based path referencing in JavaScript, just the way we have ~/ in ASP.NET?

相关标签:
12条回答
  • For ASP.net MVC Razor pages, Create a base tag like below in the <Head> tag

    <base href="http://@Request.Url.Authority@Request.ApplicationPath" />
    

    and in all your relative javascript URLs, make sure to start without a slash(/) otherwise it will refer from the root.

    For ex. create all your urls like

    "riskInfo": { url: "Content/images/RiskInfo.png", id: "RI" },
    

    or

    $http.POST("Account/GetModelList", this, request, this.bindModelList);
    
    0 讨论(0)
  • 2020-12-12 19:45

    If you want to use it in HTML Still you can use ~, see this

     href = @Url.Content("~/controllername/actionName")
    

    See the check box click event in my MVC Application

    @Html.CheckBoxFor(m=>Model.IsChecked,  
       new {@onclick=@Url.Content("~/controller/action("+ @Model.Id + ", 1)"), 
       @title="Select To Renew" })
    
    0 讨论(0)
  • 2020-12-12 19:53

    Another option that's a bit simpler and more universal would be to take the following:

    <script src="/assets/js/bootstrap.min.js"><script>
    

    and use Page.ResolveClientUrl like so:

    <script src='<%=ResolveClientUrl("~/assets/js/bootstrap.min.js")%>'></script>
    

    then regardless of what subdirectory the urls will always be rendered correctly.

    0 讨论(0)
  • 2020-12-12 19:55

    The following function will calculate the root of the currently running application. I use it to locate the absolute location of resources, when called from somewhere deep within the application tree.

        function AppRoot() {
            //
            // Returns the root of the currently running ASP application.
            // in the form: "http://localhost/TRMS40/"
            //
            //   origin: "http://localhost"
            // pathname: "/TRMS40/Test/Test%20EMA.aspx"
            //
            // usage:
            //           window.open( AppRoot() + "CertPlan_Editor.aspx?ID=" + ID);
            //
    
            var z = window.location.pathname.split('/');
    
            return window.location.origin + "/" + z[1] + "/";
        }
    
    0 讨论(0)
  • 2020-12-12 20:01

    Use base tag:

    <head>
       <base href="http://www.example.com/myapp/" />
    </head>
    

    ...

    from now any link use on this page, no matter in javascript or html, will be relative to the base tag, which is "http://www.example.com/myapp/".

    0 讨论(0)
  • 2020-12-12 20:02

    Kamarey's answer can be improved to support a dynamic base path:

    <head>    
          <base href="http://<%= Request.Url.Authority + Request.ApplicationPath%>/" />    
    </head> 
    

    This will ensure a correct root path regardless of deployment configuration.

    To be fair, this doesn't answer the original question, but it elimiates most needs for getting the root path from javascript. Simply use relative URL's everywhere, without prefixing with slash.

    Should you still need to access it from javascript, add an id attribute and use document.getElementFromId() as MiffTheFox suggested - but on the base-tag.

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