0

I have this URL: localhost:44302/api/SendMessage

Where it needs to work with GET and POST at the same time, so

localhost:44302/api/SendMessage/?destination=something@email.com&message=helloworld 

needs to work the same way that a ajax POST can work too.

Right now the Method it's like this:

 [Route("[controller]/[action]")]
    [ApiController]
    public class apiController : ControllerBase
    {
        public async Task<string> SendMessage(string destination, string message)
        {

But it only works via GET, and if add the [FROMBODY] it stops to work, is there any workaround?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Pvxtotal
  • 79
  • 7

2 Answers2

4

The action SendMessage should really be a POST. GET methods should be used to retrieve data only. If you are modifiying data on the server and/or triggering an other process it should be a POST operation.

then if you need another method to retrieve messages, it should not be the same method because it is a completely different operation

4

You could decorate the action with both [HttpGet] and [HttpPost] attributes and use [FromQuery] to bind the parameters (assuming POST will send the parameters in the URL too).

[HttpGet]
[HttpPost]    
public async Task<string> SendMessage([FromQuery]string destination, [FromQuery]string message)
{
    ...
}

I agree with the other answer in that SendMessage should really be a POST. However, if you need to access this action method using both GET and POST then perhaps this solution will work for you.

haldo
  • 14,512
  • 5
  • 46
  • 52