HTTP 404与Ajax.ActionLink [英] HTTP 404 with Ajax.ActionLink

查看:77
本文介绍了HTTP 404与Ajax.ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决404错误:

I've trouble solving a 404 error:

Global.asax.cs中的默认路由:

Default Route in Global.asax.cs:

routes.MapRoute(
       "Default", 
       "{controller}/{action}/{id}",
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

VideoController.cs:

VideoController.cs:

[HttpPost]
public ActionResult Save(int id)
{
   try
   {
     return Json(new
     {
       ID = "0"
     });
   }
   catch
   {
     return new HttpStatusCodeResult(418, "I'm a teapot");
   }
}

在我看来,ActionLink是Create.cshtml:

ActionLink in my view, Create.cshtml:

@Ajax.ActionLink("GoSave", "Save", "Video", new { id = 1 },
        new AjaxOptions { OnFailure = "Error", OnSuccess = "Saved", 
                          HttpMethod = "POST" })

操作链接的网址按预期方式呈现:/ Video / Save / 1

The url of the actionlink is rendered as expected: /Video/Save/1

当我单击链接时,我得到一个404。

When I click on the link I get an 404.

我没看到什么?

推荐答案

我敢打赌你在缺少脚本包含:

I bet you are missing a script inclusion:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

这将毫不客气地AJAX化 Ajax.ActionLink 生成结果并执行POST请求,而不是默认的GET请求。您收到404,因为您尚未包含此脚本,因此发送了GET请求,并且在视频控制器上没有能够接收GET请求的保存操作。

This will unobtrusively AJAXify the Ajax.ActionLink generated result and perform a POST request instead of the default GET one. You are getting 404 because you haven't included this script and so a GET request is sent and there is no Save action on Video controller capable of receiving a GET request.

如果您使用过 FireBug ,您将立即看到:浏览器只是在重定向并发送GET请求,而不是预期的Ajax POST请求。

If you have used FireBug you would immediately have seen this: that the browser is simply redirecting and sending a GET request instead of the expected Ajax POST request.

这篇关于HTTP 404与Ajax.ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆