title: How to get base URL of an MVC application using javascript
body: <p>How do I get the base URL using javascript?</p>
  
  <p>E.g., When I browse my site from visual studio, and if my URL is <code>http://localhost:20201/home/index</code>, I would want to get <code>http://localhost:20201</code></p>
  
  <p>If I host my site on IIS, and if my virtual directory name is MyApp and the URL is <code>http://localhost/MyApp/home/index</code>, I would want to get <code>http://localhost/MyApp</code></p>
  
  <p>I tried using <code>location.protocol</code> + <code>location.hostname</code> (and <code>location.host</code>), they work fine when i browse my site via visual studio, but when I host it on IIS, I get <code>http://localhost</code>, the /MyApp is truncated off.</p>