Search code examples
asp.net-mvchttp-postasp.net-mvc-routinghttp-put

How can you have both PUT and POST routing attributes on a controller action?


PUT and POST do a very similar thing in REST. The assumption being a POST means create a new entry and PUT means update an existing entry.

I had always assumed you could only have a single routing attribute on a controller action method, but now I have a situation where I want a method to respond to either HttpPost or HttpPut.

Update:

Tried a few variations and the actions were not hit if more than one routing attribute was applied. Like these:

[HttpPost]
[HttpPut]
public ActionResult Include(int id, int order, int parent)
{
    return "...some result";
}

[HttpPost, HttpPut]
public ActionResult Include(int id, int order, int parent)
{
    return "...some result";
}

The question now is: How do you respond to both PUT and POST requests in the same controller action?


Solution

  • There's builtin way to do that. Use AcceptVerbsAttribute

    [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
    public ActionResult Include()
    {
    }