Within my ASP.NET MVC (5) project, I utilize T4MVC to avoid using magic strings in my views.
While this approach works well, there are instances where I require URLs in my JavaScript or TypeScript code, particularly for AJAX requests.
Currently, I rely on Razor code within my views to assign URLs to JavaScript variables:
window['myJavaScripVariableName'] = '@Url.Action(MVC.Progress.MyActionMethodName())';
These variables are then accessed in .js and .ts files like so:
$.get(window['myJavaScripVariableName'], { operationCategory: this.operationCategory })
.done((data) => {...}
However, managing multiple URLs in this manner can become tedious, not to mention the risk of typos when referencing them.
Is there a way to dynamically generate URLs in JavaScript / TypeScript without hardcoding them?