Search code examples
c#apiasp.net-core.net-corefrombodyattribute

How to hit a HttpPost-action with a parameter?


I have an API with a

[HttpPost("InsertSomething")]
public async Task<IActionResult> InsertSomething(Guid id, [FromBody] MyModel model)
{
   try
   {
      ...
      Guid somethingId = await _myService.Insert(id, enz..)

and when I wanna calling this Action

HttpResponseMessage result = await client.PostAsync("/MyContr/InsertSomething", content);

then i will come in the Action of the API.

... and when you add something like this

HttpResponseMessage result = await client.PostAsync($"/MyContr/InsertSomething?id=BlaBlaBla", content);

then we did not comein the Action of the API.

How can I solve this problem?


Solution

  • You can change your endpoint like this:

     [HttpPost("InsertSomething")]
     public async Task<IActionResult> InsertSomething([FromQuery]Guid id, [FromBody] MyModel model)
    

    also, make sure that you are passing correct guid.