C# 在同一控制器中具有相同操作名称的 GET 和 POST 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9552761/
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 methods with the same Action name in the same Controller
提问by Ricardo Polo Jaramillo
Why is this incorrect?
为什么这是不正确的?
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
[HttpPost]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
}
How can I have a controlller thas answer one thing when is "getted" and one when is "posted"?
我怎样才能让控制器在“获取”和“发布”时回答一件事?
采纳答案by BrokenGlass
Since you cannot have two methods with the same name and signature you have to use the ActionNameattribute:
由于您不能有两个具有相同名称和签名的方法,因此您必须使用该ActionName属性:
[HttpGet]
public ActionResult Index()
{
// your code
return View();
}
[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost()
{
// your code
return View();
}
Also see "How a Method Becomes An Action"
另请参阅“方法如何变成动作”
回答by Mason
Can not multi action same name and same parameter
不能多动作同名同参数
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(int id)
{
return View();
}
althought int id is not used
虽然没有使用 int id
回答by Jeff Siver
To answer your specific question, you cannot have two methods with the same name and the same arguments in a single class; using the HttpGet and HttpPost attributes doesn't distinguish the methods.
要回答您的具体问题,在一个类中不能有两个具有相同名称和相同参数的方法;使用 HttpGet 和 HttpPost 属性不区分方法。
To address this, I'd typically include the view model for the form you're posting:
为了解决这个问题,我通常会包含您发布的表单的视图模型:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
Some Code--Some Code---Some Code
return View();
}
[HttpPost]
public ActionResult Index(formViewModel model)
{
do work on model --
return View();
}
}
回答by Jeff Siver
While ASP.NET MVC will allow you to have two actions with the same name, .NET won't allow you to have two methods with the same signature - i.e. the same name and parameters.
虽然 ASP.NET MVC 允许您拥有两个具有相同名称的操作,但 .NET 不允许您拥有两个具有相同签名的方法 - 即相同的名称和参数。
You will need to name the methods differently use the ActionName attribute to tell ASP.NET MVC that they're actually the same action.
您将需要以不同的方式命名方法,使用 ActionName 属性告诉 ASP.NET MVC 它们实际上是相同的操作。
That said, if you're talking about a GET and a POST, this problem will likely go away, as the POST action will take more parameters than the GET and therefore be distinguishable.
也就是说,如果您在谈论 GET 和 POST,这个问题可能会消失,因为 POST 操作将采用比 GET 更多的参数,因此是可区分的。
So, you need either:
所以,你需要:
[HttpGet]
public ActionResult ActionName() {...}
[HttpPost, ActionName("ActionName")]
public ActionResult ActionNamePost() {...}
Or,
或者,
[HttpGet]
public ActionResult ActionName() {...}
[HttpPost]
public ActionResult ActionName(string aParameter) {...}
回答by Mark
You can't have multiple actions with the same name. You could add a parameter to one method and that would be valid. For example:
您不能有多个具有相同名称的操作。您可以向一个方法添加一个参数,这将是有效的。例如:
public ActionResult Index(int i)
{
Some Code--Some Code---Some Code
return View();
}
There are a few ways to do to have actions that differ only by request verb. My favorite and, I think, the easiest to implement is to use the AttributeRoutingpackage. Once installed simply add an attribute to your method as follows:
有几种方法可以使操作仅因请求动词而有所不同。我最喜欢并且我认为最容易实现的是使用AttributeRouting包。安装后,只需向您的方法添加一个属性,如下所示:
[GET("Resources")]
public ActionResult Index()
{
return View();
}
[POST("Resources")]
public ActionResult Create()
{
return RedirectToAction("Index");
}
In the above example the methods have different names but the action name in both cases is "Resources". The only difference is the request verb.
在上面的示例中,方法具有不同的名称,但两种情况下的操作名称都是“Resources”。唯一的区别是请求动词。
The package can be installed using NuGet like this:
该包可以使用 NuGet 安装,如下所示:
PM> Install-Package AttributeRouting
PM> Install-Package AttributeRouting
If you don't want the dependency on the AttributeRouting packages you could do this by writing a custom action selector attribute.
如果您不想依赖 AttributeRouting 包,您可以通过编写自定义操作选择器属性来实现。
回答by jocull
I like to accept a form post for my POST actions, even if I don't need it. For me it just feels like the right thing to do as you're supposedly posting something.
我喜欢为我的 POST 操作接受一个表单帖子,即使我不需要它。对我来说,这感觉就像是正确的事情,因为你应该发布一些东西。
public class HomeController : Controller
{
public ActionResult Index()
{
//Code...
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
//Code...
return View();
}
}
回答by RredCat
You received the good answer to this question, but I want to add my two cents. You could use one method and process requests according to request type:
你收到了这个问题的好答案,但我想补充我的两分钱。您可以使用一种方法并根据请求类型处理请求:
public ActionResult Index()
{
if("GET"==this.HttpContext.Request.RequestType)
{
Some Code--Some Code---Some Code for GET
}
else if("POST"==this.HttpContext.Request.RequestType)
{
Some Code--Some Code---Some Code for POST
}
else
{
//exception
}
return View();
}
回答by Moacir
Today I was checking some resources about the same question and I got an example very interesting.
今天我正在检查一些关于同一问题的资源,我得到了一个非常有趣的例子。
It is possible to call the same methodby GET and POST protocol, but you need to overload the parameters like that:
可以通过 GET 和 POST 协议调用相同的方法,但是您需要像这样重载参数:
@using (Ajax.BeginForm("Index", "MyController", ajaxOptions, new { @id = "form-consulta" }))
{
//code
}
The action:
那个行动:
[ActionName("Index")]
public async Task<ActionResult> IndexAsync(MyModel model)
{
//code
}
By default a method without explicit protocol is GET, but in that case there is a declared parameter which allows the method works like a POST.
默认情况下,没有显式协议的方法是 GET,但在这种情况下,有一个声明的参数允许该方法像 POST 一样工作。
When GET is executed the parameter does not matter, but when POST is executed the parameter is required on your request.
执行 GET 时参数无关紧要,但执行 POST 时,您的请求需要参数。

