title: MVC "~" path in javascript
body: <p>I use JavaScript code to call an MVC web API. It works fine when the current path is:</p>
  
  <p><code>http://localhost/myApp/Administrator</code></p>
  
  <p>but it fails when current path is:</p>
  
  <p><code>http://localhost/myApp/Administrator/</code></p>
  
  <p>I get the error <code>The resource cannot be found</code>. Below is the code:</p>
  
  <pre><code>$.getJSON("api/UserApi",
      function (data) {
          ...               
      });
  </code></pre>
  
  <p>I don't want to use an absolute URL in the code, e.g.:</p>
  
  <pre><code>$.getJSON("http://localhost/myApp/api/UserApi",          
      function (data) {
          ...    
      });
  </code></pre>
  
  <p>The absolute URL does work fine, but lacks flexibility. Is there a way to do the same thing as below?</p>
  
  <pre><code>$.getJSON("~/api/UserApi",          
      function (data) {
          ...
      });
  </code></pre>
  
  <p>ASP.NET supports the replacement of the "~" character with the current application's root path, e.g.:</p>
  
  <p><code>http://localhost/myApp</code></p>
  
  <p>However, the "~" character is not supported in JavaScript files. How do I accomplish the same thing?</p>
  
  <p>The JavaScript is in a standalone file that cannot use ASP.NET statements like <code>Url.Content</code>. Is there a better way to do it?</p>
  
  <p>I've found the following method. Are there any better solutions?:</p>
  
  <p>1) Write the code below in a .cshtml file</p>
  
  <pre><code>&lt;script type="text/javascript"&gt; 
      var currentDomain = '@Url.Content("~")';
  &lt;/script&gt;
  </code></pre>
  
  <p>2) Read the <code>currentDomain</code> variable from the .js file:</p>
  
  <pre><code>$.getJSON(currentDomain + "/api/UserApi",          
      function (data) {
          ...        
  });
  </code></pre>