What's the best way to avoid hardcoding URL's in JavaScript (primarily used when making AJAX calls)?
In the past:
- Render JavaScript variable with result of
@Url.Action
or@Url.RouteUrl
- Pass result of
@Url.Action
or@Url.RouteUrl
to JavaScript in init/ctor.
Is there a better way?
It would be good to do something like this:
var url = $.routes("actionName", "controllerName") // or "routeName" for named routes
$.post(url, { id = 1 }, function() { //.. });
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).
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)
How do people handle this?