Here's a pattern I've been using. It's a bit more steps, but I like that all of my urls are in one organized location in the View.
At the bottom of my View I will include a Scripts Section that contains the urls like so:
@section Scripts
{
<script type="text/javascript">
myJavaScriptObject.firstUrl = '@Url.Action("Action1", "Controller", new {id = Model.Id})';
myJavaScriptObject.secondUrl = '@Url.Action("Action2", "Controller", new {id = Model.Id})';
</script>
}
Inside of my JavaScript Class (which is in an external file) I will reference the url like so:
var myJavaScriptObject = {
firstUrl: '',
secondUrl: '',
docReady: function() {
$.get(myJavaScriptObject.firstUrl, function(data) { do something... });
}
}
I realize the entries don't need to be referenced inside of the Class, but I like to have them there for my own housekeeping.
url = '~' + "@Url.Action("Action", "Controller")"@Url.Action("Action", "Controller")output is@Url.Action("Action", "Controller")so your suggestion output is[email protected]("Action", "Controller")I needSite/subFolder/controller/Action