Home > Blockchain >  Why list of object is null in query parameters
Why list of object is null in query parameters

Time:02-02

I'am trying to get list of object from query parameter but it's null.

I Added [FromQuery] before it but it's still null

Controller :

[HttpGet("filter")]
public async Task<IActionResult> GetFilteredSperms([FromQuery] List<SpermFilterDTO> filters, [FromQuery] string name)
    {
        var result = await _mediator.Send(new GetSpermsByFilterQuery { Filters = filters });

        foreach (var item in filters)
        {
            Console.WriteLine($"Index is {item.Index}");
        }

        Console.WriteLine($"Name is {name}");

        return Ok(result);
    }

name property is Ok but filters is empty.

CodePudding user response:

It is not recommended (nor really supported) to place complex objects in the query string. A GET action supports a request body and a GET request with a body is not forbidden by any specification or standard.

Thus, moving the object from the query to the body will allow your action to function as intended.

...
public async Task<IActionResult> GetFilteredSperms([FromBody] List<SpermFilterDTO> filters, [FromQuery] string name)
...
  • Related