asp.net-mvc ASP.NET MVC:带有参数的 RedirectToAction POST 操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16643414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:51:11  来源:igfitidea点击:

ASP.NET MVC: RedirectToAction with parameters to POST Action

asp.net-mvcasp.net-mvc-4

提问by Rosdi Kasim

This question has been asked here:

这个问题在这里被问到:

RedirectToAction with parameter

带参数的 RedirectToAction

But what if I have two actions with the same name but different parameters? How do I redirect to the POST Termsaction instead of the GET Termsaction.

但是如果我有两个名称相同但参数不同的操作怎么办?如何重定向到 POSTTerms操作而不是 GETTerms操作。

public ActionResult Terms() {
    //get method
}

[HttpPost]
public ActionResult Terms(string month, string year, int deposit = 0, int total = 0) {
    //process POST request
}

回答by Rosdi Kasim

Nevermind guys, actually I could just call the method directly instead of using RedirectToActionlike so:

没关系伙计们,实际上我可以直接调用该方法而不是RedirectToAction像这样使用:

return Terms(month, year, deposit, total);

Instead of:

代替:

return RedirectToAction("Terms", {month, year, deposit, total});

回答by Sarel Esterhuizen

You are correct that you can call the method directly, but I would highly suggest that you rethink your architecture/implementation.

您可以直接调用该方法是正确的,但我强烈建议您重新考虑您的架构/实现。

The HTTP Protocol embraces the idea of safe and unsafe verbs. Safe verbs like GET are not suppose to modify the state of the server in any way, whilst Unsafe verbs like POST, PUT do modify state. By you GET calling the POST method you are violating this principle since it is not inconceivable that your POST is going to be modifying state.

HTTP 协议包含安全和不安全动词的概念。像 GET 这样的安全动词不应该以任何方式修改服务器的状态,而像 POST、PUT 这样的不安全动词会修改状态。通过 GET 调用 POST 方法,您违反了这一原则,因为您的 POST 将修改状态并非不可想象。

Also best practice dictates that you should limits the verbs on all your actions so if the first 'Terms' method is meant as a GET, then also add the HttpGet attribute to it to prevent other Http actions from being accepted by the server for the action.

最佳实践还规定,您应该限制所有操作的动词,因此如果第一个“条款”方法是 GET,则还要向其添加 HttpGet 属性以防止服务器接受其他 Http 操作以进行该操作.