$ajax 内的相对 URL 与 asp.net mvc 3
我知道可以使用此函数
@Url.Action("MyInfo", "Home")
来避免对 url 进行硬编码,但我的 $.ajax
调用位于单独的 .js 文件中。上面的方法还有效吗?
据我所知,@Url.Action
只能在 Razor 文件内工作。但考虑到我们建议使用非侵入式 JS,我不太确定如何使用 @Url.Action
。
请指教。
I know one can use this function
@Url.Action("MyInfo", "Home")
to avoid the hardcoding of urls, but my $.ajax
calls are in a separate .js file. Would the above still work?
From my knowledge, the @Url.Action
will only work inside the Razor file. But considering that we are advised to use non-obtrusive JS, I am not quite sure how I would use the @Url.Action
.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。
您的知识是正确的。
您可以在您正在不显眼地增强的某些 DOM 元素上使用 HTML5 data-* 属性(除非该元素已经是
然后在您单独的 javascript 文件中:
No.
Your knowledge is correct.
You could use HTML5 data-* attributes on some DOM element that you are unobtrusively enhancing (unless this element is already a
<form>
or an anchor in which case it already contains an url):and then in your separate javascript file:
为相对路径添加函数参数。例如,在您的 View:
和外部 js 文件中:
Add a function parameter for the relative paths. E.g., in your View:
and in your external js file: