developer82
developer82

Reputation: 13713

ASP.NET Web API method for both GET and POST

I have the following method in my API:

[HttpGet]
public HttpResponseMessage ExecuteCommand()
{
    // logic
}

This method currently serves only the http GET method. I would also like it to respond to http POST method - Is that possible? or do I have to duplicate the method?

Thanks

Upvotes: 4

Views: 1502

Answers (1)

Niklas
Niklas

Reputation: 13135

You can do it like this

[AcceptVerbs("Get", "Post")]
public HttpResponseMessage ExecuteCommand()
{
    // logic
}

This is possible since the constructor looks like this, and takes an array of strings.

public AcceptVerbsAttribute(
    params string[] verbs
)

Upvotes: 7

Related Questions