0

In an ASP.NET MVC application, we call web service and web API methods from JavaScript files. Whenever the url gets changed, we have to modify the url in many .js files.

As we access the url in JavaScript, is there anyway to set it globally like web.config in .NET?

Thanks.

j08691
  • 204,283
  • 31
  • 260
  • 272
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
  • well you could have api for url retrieval, so you'd only have to set it on server, and it's not specific what url are you pointing to – Marko Mackic Jul 26 '16 at 01:55
  • http://stackoverflow.com/questions/34360537/how-do-i-make-js-know-about-the-application-root – Shyju Jul 26 '16 at 02:16

2 Answers2

0

You can just have this or something similar in your view:

<script>
  window.apiUrl = '<%=ConfigurationManager.AppSettings["apiUrl"]%>';
</script>

(or if it's Razor?...)

<script>
  window.apiUrl = '@(ConfigurationManager.AppSettings["apiUrl"])';
</script>

That way, your other scripts can simply reference:

var url = window.apiUrl;
Jacob
  • 77,566
  • 24
  • 149
  • 228
0

You can declare a global variable in the view(call VewBag in the Controller),and This variable in the configuration

Jade
  • 1
  • 1