A quick summary of the situation:
快速总结一下情况:
In my View I have this piece of Razor code:
在我看来,我有一段剃刀的代码:
@{
ViewBag.Title = "Index";
AjaxOptions options = new AjaxOptions();
options.HttpMethod = "POST";
}
...
@Ajax.ActionLink("Linkname", "CreateChallenge", new { challengedId = Model.UserId },options);
Than in my controller:
比我的控制器:
[Authorize]
[HttpPost]
public string CreateChallenge(string challengedId)
{
ChallengeRepository.CreateChallenge(challengedId);
return "Sendend!";
}
I get an 'Resource not found' error when I click the link but when I remove the [HttpPost] attribute everything works fine. But I want a POST method. I have looked around and found some similar problems but none of the solutions worked for me.
当我点击链接时,会出现“资源未找到”的错误,但是当我删除[HttpPost]属性时,一切正常。但是我想要一个POST方法。我环顾四周,发现了一些类似的问题,但没有一个解决方案对我有效。
1 个解决方案
#1
8
UPDATE Spoke too soon, remembered that you need the jQuery.Ajax.Unobtrusive http://www.nuget.org/packages/jQuery.Ajax.Unobtrusive/ package
更新太快,记住您需要jQuery.Ajax。不引人注目的http://www.nuget.org/packages/jQuery.Ajax.Unobtrusive/包
If you install this and reference it in you view it should work, it did i my OOTB test :)
如果你安装了这个,并在你的视图中引用它,它应该可以工作,我的OOTB测试:)
Did a quick test myself, it seems you cant use Ajax.ActionLink to issue a POST request, it does a GET even though you set POST in AjaxOptions. You can see this if you use fiddlr to monitor the traffic.
我自己做了一个快速测试,好像你不能用Ajax。ActionLink发出一个POST请求,即使你在AjaxOptions中设置POST,它也会执行GET。如果您使用fiddlr监视流量,您可以看到这一点。
You can also use the Postman extension for Chrome to test it, you will see that the action method actually behaves as it should when you POST to it. But you get the 404 because it actually does a GET
您还可以使用Postman扩展来测试它,您将看到操作方法实际上是在您发布到它时的行为。但你会得到404因为它实际上是一个get
If it were me I would use jQuery to do the post. You can see more here
http://api.jquery.com/jquery.ajax/
如果是我,我会用jQuery来写这篇文章。您可以在这里看到更多http://api.jquery.com/jquery.ajax/
#1
8
UPDATE Spoke too soon, remembered that you need the jQuery.Ajax.Unobtrusive http://www.nuget.org/packages/jQuery.Ajax.Unobtrusive/ package
更新太快,记住您需要jQuery.Ajax。不引人注目的http://www.nuget.org/packages/jQuery.Ajax.Unobtrusive/包
If you install this and reference it in you view it should work, it did i my OOTB test :)
如果你安装了这个,并在你的视图中引用它,它应该可以工作,我的OOTB测试:)
Did a quick test myself, it seems you cant use Ajax.ActionLink to issue a POST request, it does a GET even though you set POST in AjaxOptions. You can see this if you use fiddlr to monitor the traffic.
我自己做了一个快速测试,好像你不能用Ajax。ActionLink发出一个POST请求,即使你在AjaxOptions中设置POST,它也会执行GET。如果您使用fiddlr监视流量,您可以看到这一点。
You can also use the Postman extension for Chrome to test it, you will see that the action method actually behaves as it should when you POST to it. But you get the 404 because it actually does a GET
您还可以使用Postman扩展来测试它,您将看到操作方法实际上是在您发布到它时的行为。但你会得到404因为它实际上是一个get
If it were me I would use jQuery to do the post. You can see more here
http://api.jquery.com/jquery.ajax/
如果是我,我会用jQuery来写这篇文章。您可以在这里看到更多http://api.jquery.com/jquery.ajax/