11

If I create a controller action and do not decorate it with AcceptVerbs, HttpPost or HttpGet. What is the default behaviour?

Does the action allow any access method or does it default to GET?

John Mills
  • 10,020
  • 12
  • 74
  • 121

2 Answers2

16

It's accessible via any verb.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • 3
    @marcind tested it and it works like you answered. It would be great to have some links to official documentation that states this. – broadband Aug 11 '15 at 12:50
4

In Web API 2.1:

it depends on the name of the action. If the action starts with "Get*" then it will default to only accept GET requests. If it starts with "Put*" then it will default to only accept PUT requests. Same with POST.

If it doesn't start with any known verb then it will default to only accept POST.

Here are the results of my testing:

public class BlahController : ApiController
{
    // only allows GET
    public string GetSomething() { return "GetSomething blah"; }

    // only allows PUT
    public string PutSomething() { return "PutSomething blah"; }

    // only allows POST
    public string PostSomething() { return "PostSomething blah"; }

    // only allows POST
    public string Fleabag() { return "Fleabag blah"; }
}
demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
  • It's quite strange, because I have just received: "The requested resource does not support http method 'GET'" for an action without attributes (tested in WebApi 2.2). – Xavier Egea Sep 01 '14 at 10:57
  • Try to test it with an action method distinct from "Get()" or "Post()" or "GetById()", because in these cases, if there is no Accepted verb attribute, the http method will be resolved automatically from the name. – Xavier Egea Sep 01 '14 at 11:22
  • Contrary to my answer, AFAICT if the method name starts with "Get" then it will default to only accept GET requests. Otherwise it defaults to accept any verb. – demoncodemonkey Sep 01 '14 at 11:36
  • 1
    I've tested with Dummy() action/method and it's only accepting POST. Have you tried it? – Xavier Egea Sep 01 '14 at 11:55
  • @Freerider it depends on the name of the action method, see my edit for details. – demoncodemonkey Sep 01 '14 at 12:00
  • Hmm I'm gonna do some more checks, something not right. – demoncodemonkey Sep 01 '14 at 12:08
  • There was a bug in my testing causing me to misunderstand how it works. I updated my edited answer once more, pretty sure it's nailed now. – demoncodemonkey Sep 01 '14 at 12:17
  • 2
    I've reached the same conclusion. It would be nice to have an offical documentation with this info. – Xavier Egea Sep 01 '14 at 13:19
  • The question is tagged with [tag:asp.net-mvc-2], Web API is an entirely different beast. While similar to MVC routing, it does behave differently for certain things. – user247702 Sep 15 '15 at 07:26
  • @Stijn I updated my answer to make it clearer that I was talking about WebAPI and not MVC. – demoncodemonkey Sep 15 '15 at 11:15