.netrestwebapi2

WebAPI .NET - POST request filter by parameters


I use POST to create messages and to update them (I need to use POST, not PUT). The API has the following instructions:

POST /api/message

POST /api/message?update_message

How can I difference between both? Guess I have to do an if in the function:

[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Handle()
{}

checking if the request contains the parameter update_message.

Any idea on how to solve that? Thanks.


Solution

  • Finally solved with both actions separated with this syntax:

    For POST /api/message?update_message=true requests:

    [HttpPost]
    [Route("api/message")]
    public async Task<HttpResponseMessage> Update(bool update_message, [FromBody] message m)
    {}
    

    For POST /api/message requests:

    [HttpPost]
    [Route("api/message")]
    public async Task<HttpResponseMessage> Create()
    {}