asp.net-mvc 如何使用 POST 动词重定向到页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/129335/
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
How do you redirect to a page using the POST verb?
提问by Chris Pietschmann
When you call RedirectToActionwithin a controller, it automatically redirects using an HTTP GET. How do I explicitly tell it to use an HTTP POST?
当您RedirectToAction在控制器内调用时,它会使用 HTTP GET 自动重定向。我如何明确告诉它使用 HTTP POST?
I have an action that accepts both GET and POST requests, and I want to be able to RedirectToActionusing POST and send it some values.
我有一个接受 GET 和 POST 请求的操作,我希望能够RedirectToAction使用 POST 并向其发送一些值。
Like this:
像这样:
this.RedirectToAction(
"actionname",
new RouteValueDictionary(new { someValue = 2, anotherValue = "text" })
);
I want the someValueand anotherValuevalues to be sent using an HTTP POST instead of a GET. Does anyone know how to do this?
我希望使用 HTTP POST 而不是 GET 发送someValue和anotherValue值。有谁知道如何做到这一点?
采纳答案by Eli Courtwright
HTTP doesn't support redirection to a page using POST. When you redirect somewhere, the HTTP "Location" header tells the browser where to go, and the browser makes a GET request for that page. You'll probably have to just write the code for your page to accept GET requests as well as POST requests.
HTTP 不支持使用 POST 重定向到页面。当你重定向到某个地方时,HTTP“Location”标头会告诉浏览器去哪里,浏览器会对该页面发出 GET 请求。您可能只需要为您的页面编写代码来接受 GET 请求和 POST 请求。
回答by Jason Bunting
For your particular example, I would just do this, since you obviously don't care about actually having the browser get the redirect anyway (by virtue of accepting the answer you have already accepted):
对于您的特定示例,我会这样做,因为您显然并不关心实际上让浏览器获得重定向(通过接受您已经接受的答案):
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index() {
// obviously these values might come from somewhere non-trivial
return Index(2, "text");
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int someValue, string anotherValue) {
// would probably do something non-trivial here with the param values
return View();
}
That works easily and there is no funny business really going on - this allows you to maintain the fact that the second one really only accepts HTTP POST requests (except in this instance, which is under your control anyway) and you don't have to use TempData either, which is what the link you posted in your answer is suggesting.
这很容易工作,并且没有真正发生有趣的事情 - 这使您可以保持这样一个事实,即第二个确实只接受 HTTP POST 请求(除了在这种情况下,无论如何它都在您的控制之下)并且您不必也可以使用 TempData,这就是您在答案中发布的链接所建议的。
I would love to know what is "wrong" with this, if there is anything. Obviously, if you want to really have sent to the browser a redirect, this isn't going to work, but then you should ask why you would be trying to convert that regardless, since it seems odd to me.
我很想知道这有什么“问题”,如果有的话。显然,如果您真的想向浏览器发送重定向,这是行不通的,但是您应该问为什么无论如何都要尝试转换它,因为这对我来说似乎很奇怪。
Hope that helps.
希望有帮助。
回答by Otto Kanellis
If you want to pass data between two actions during a redirect without include any data in the query string, put the model in the TempData object.
如果要在重定向期间在两个操作之间传递数据而不在查询字符串中包含任何数据,请将模型放在 TempData 对象中。
ACTION
行动
TempData["datacontainer"] = modelData;
TempData["datacontainer"] = modelData;
VIEW
看法
var modelData= TempData["datacontainer"] as ModelDataType;
TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this.
TempData 是一个非常短暂的实例,您应该只在当前和后续请求期间使用它!由于 TempData 以这种方式工作,您需要确定下一个请求将是什么,并且重定向到另一个视图是唯一可以保证这一点的时间。
Therefore, the only scenario where using TempData will reliably work is when you are redirecting.
因此,使用 TempData 可靠工作的唯一情况是在您重定向时。
回答by vicky
try this one
试试这个
return Content("<form action='actionname' id='frmTest' method='post'><input type='hidden' name='someValue' value='" + someValue + "' /><input type='hidden' name='anotherValue' value='" + anotherValue + "' /></form><script>document.getElementById('frmTest').submit();</script>");
回答by Yitzhak Weinberg
I would like to expand the answer of Jason Bunting
我想扩大杰森邦廷的答案
like this
像这样
ActionResult action = new SampelController().Index(2, "text");
return action;
And Eli will be here for something idea on how to make it generic variable
Eli 将在这里寻求有关如何使其成为通用变量的想法
Can get all types of controller
可以获得所有类型的控制器

