且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动作相对于电流控制器MVC阿贾克斯

更新时间:2023-02-25 16:37:30

如果你有这个直入你的看法,你可以做

If you got this straight into your view, you could do

 $.ajax({
      url: '@Url.Action("Save")',
      type: "GET",
      // .. etc 
 });

如果没有,JavaScript是在外部文件,您可以附加与Url.Action$c$c>以元素作为数据 - HTML5属性。然后做AJAX调用之前动态读取属性值。

If not, and javascript is in external file, you could attach url generated with Url.Action to element as data-? html5 attribute. And then dynamically read that attribute value before doing ajax call.

<input type="text" data-save-action-url="@Url.Action("Save")" />

您应该的从不硬code网址在asp.net mvc的。始终使用 Url.Action 。生成URL时,永远返回根据它正确的值它检查你的路由配置。如果你硬code的URL,当您更改路由配置您的应用程序可能无法使用。你将不得不手动改变每一个URL在你的应用程序。

You should never hardcode url's in asp.net mvc. Always use Url.Action. It inspects your routing configuration when generating urls, and will always return correct value according to it. If you hardcode urls, your application may become unusable when you change routing configuration. And you will have to change every single url in you application manually.