在我的 ASP.NET MVC (5) 应用程序中,我使用 T4MVC 来防止魔术字符串出现在我的 View 中。
但是,我的 JavaScript 或 TypeScript 代码中通常需要 Url,尤其是在执行 AJAX 请求时。
目前,我在 View 中使用 Razor 代码来设置 JavaScript 变量来保存 URL:
window['myJavaScripVariableName'] = '@Url.Action(MVC.Progress.MyActionMethodName())';
然后从 .js 和 .ts 代码文件访问它们,例如:
$.get(window['myJavaScripVariableName'], { operationCategory: this.operationCategory })
.done((data) => {...
但是,当有数十个 URL 需要传递时,这很快就变得无聊,更不用说我在 myJavaScripVariableName
的两次出现之一中犯了拼写错误的可能性。
是否支持不在 JavaScript/TypeScript 中硬编码 Url 文字?
最佳答案
However this quickly became boring when there are dozens of Url to pass, not talking about the possibility that I make a typo in myJavaScripVariableName in one of its two appearances.
您需要查看 C# -> TypeScript 代码生成。对于 C#,有多种选项,最流行的是 http://type.litesolutions.net/
关于javascript - T4MVC 对 TypeScript 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921760/