Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 587
developer82 Avatar asked Oct 14 '25 19:10

developer82


1 Answers

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
)
like image 199
Niklas Avatar answered Oct 17 '25 21:10

Niklas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!