带有Ajax.ActionLink的HTTP 404

And*_*rew 5 asp.net-mvc routing asp.net-mvc-3

我在解决404错误时遇到了麻烦:

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

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

VideoController.cs:

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

在我看来,Action.cshtml中的ActionLink:

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

actionlink的url按预期呈现:/ Video/Save/1

当我点击链接时,我得到了404.

我没看到什么?

Dar*_*rov 11

我打赌你错过了一个script包含:

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

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

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