What is the default behaviour of a controller action not marked with AcceptVerbs, HttpGet or HttpPost?

人盡茶涼 提交于 2019-11-27 06:51:35

问题


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?


回答1:


It's accessible via any verb.




回答2:


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"; }
}


来源:https://stackoverflow.com/questions/3672379/what-is-the-default-behaviour-of-a-controller-action-not-marked-with-acceptverbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!