简体   繁体   English

在同一个Controller中具有相同Action名称的GET和POST方法

[英]GET and POST methods with the same Action name in the same Controller

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"? 我怎样才能让控制器在“getted”时回答一件事,在“发布”时回答一件事?

Since you cannot have two methods with the same name and signature you have to use the ActionName attribute: 由于您不能使用具有相同名称和签名的两个方法,因此必须使用ActionName属性:

    [HttpGet]
    public ActionResult Index()
    {
        Some Code--Some Code---Some Code
        return View();
    }

    [HttpPost]
    [ActionName("Index")]
    public ActionResult IndexPost()
    {
        Some Code--Some Code---Some Code
        return View();
    }

Also see "How a Method Becomes An Action" 另请参阅“方法如何成为行动”

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 - ie 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) {...}

I like to accept a form post for my POST actions, even if I don't need it. 我喜欢接受我的POST动作的表格帖子,即使我不需要它。 For me it just feels like the right thing to do as you're supposedly posting something . 对我来说,感觉就像你应该发布的东西一样正确。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        //Code...
        return View();
    }

    [HttpPost]
    public ActionResult Index(FormCollection form)
    {
        //Code...
        return View();
    }
}

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();
    }

}

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

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 AttributeRouting package. 我最喜欢的,我认为最容易实现的是使用AttributeRouting包。 Once installed simply add an attribute to your method as follows: 安装完成后,只需在方法中添加一个属性,如下所示:

  [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. 唯一的区别是请求动词。

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包,则可以通过编写自定义操作选择器属性来完成此操作。

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();
}

Today I was checking some resources about the same question and I got an example very interesting. 今天我正在检查有关同一问题的一些资源,我得到了一个非常有趣的例子。

It is possible to call the same method by 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时,您的请求需要参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控制器方法具有相同名称 - Controller Methods with same name @ Url.Action(“ Action”,“ Controller”)调用具有相同名称的post和get方法 - @Url.Action(“Action”,“Controller”) calls both post and get method with same name MVC ActionLink转到具有相同名称的Get Action Controller而不是Post Controller - MVC ActionLink goes to Get Action Controller Instead of Post Controller with the same name POST 和 GET 方法在同一个按钮上 - POST and GET methods on the same button 具有不同返回类型的操作方法的相同名称 - Same Name For Action Methods With Different Return Types 获取具有相同名称的方法的集合 - Get collection of methods with the same name 在同一个Controller中使用Get和Post - Use Get And Post in the same Controller 与控制器操作同名的虚拟目录 - virtual directory with same name as controller action ASP.Net 中具有相同路由名称的 Post Action 方法重载 Web API Controller - Post Action method overload with same route name in ASP.Net Web API Controller 如何在同一控制器中获得MVC4 Web API以支持HTTP动词和“操作”方法? - How do I get MVC4 Web API to support HTTP Verbs and “Action” methods in the same controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM