title: Handling ASP.NET MVC Routing in External JavaScript
body: <p>What's the best way to avoid hardcoding URL's in JavaScript (primarily used when making AJAX calls)?</p>
  
  <p>In the past:</p>
  
  <ol>
  <li>Render JavaScript variable with result of <code>@Url.Action</code> or <code>@Url.RouteUrl</code></li>
  <li>Pass result of <code>@Url.Action</code> or <code>@Url.RouteUrl</code> to JavaScript in init/ctor.</li>
  </ol>
  
  <p>Is there a better way?</p>
  
  <p>It would be good to do something like this:</p>
  
  <pre><code>var url = $.routes("actionName", "controllerName") // or "routeName" for named routes
  $.post(url, { id = 1 }, function() { //.. });
  </code></pre>
  
  <p>Which of course isn't really possible (JavaScript doesn't have direct access the to the ViewContext and thus doesn't have access to the route tables).</p>
  
  <p>But i'm wondering if there's a way i can kind of setup my own "route table" for JavaScript, with only the ones i know it would need? (e.g i set it up in the View)</p>
  
  <p>How do people handle this?</p>