asp.net-mvc GET 和 POST 到 ASP.NET MVC 中的相同控制器操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3232013/
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
GET and POST to same Controller Action in ASP.NET MVC
提问by Cranialsurge
I'd like to have a single action respond to both Gets as well as Posts. I tried the following
我希望对 Gets 和 Posts 都有一个响应。我尝试了以下
[HttpGet]
[HttpPost]
public ActionResult SignIn()
That didn't seem to work. Any suggestions ?
那似乎不起作用。有什么建议 ?
回答by Ryan Bair
回答by Kurt Schindler
Actions respond to both GETs and POSTs by default, so you don't have to specify anything:
默认情况下,操作会同时响应 GET 和 POST,因此您无需指定任何内容:
public ActionResult SignIn()
{
//how'd we get here?
string method = HttpContext.Request.HttpMethod;
return View();
}
Depending on your need you could still perform different logic depending on the HttpMethod by operating on the HttpContext.Request.HttpMethod value.
根据您的需要,您仍然可以通过操作 HttpContext.Request.HttpMethod 值,根据 HttpMethod 执行不同的逻辑。
回答by Neil Outler
[HttpGet]
public ActionResult SignIn()
{
}
[HttpPost]
public ActionResult SignIn(FormCollection form)
{
}