1

I have a file for my javascript separate from my web pages. However what this appears to mean is that I cannot write the following line;

var url = '@Url.Action("AddTrade", "DataService")';

I am reluctant to hard code the url in case it is different when I deploy it.

So what should I do about this?

scott-pascoe
  • 1,463
  • 1
  • 13
  • 31
arame3333
  • 9,887
  • 26
  • 122
  • 205

1 Answers1

1

I use an object to print all the urls I'll use from the scripts. This code goes into the _Layout or any view.

Like this:

<script type="text/javascript">
    windows.Urls = {};
    window.Urls["SomeKey"] = "@Url.Action("AddTrade", "DataService")";
</script>

Then, from the scripts, you use:

var url = window.Urls["SomeKey"];
// Or even easier:
var url = Urls.SomeKey
Diego
  • 16,436
  • 26
  • 84
  • 136