1

I have this class

public enum Comparison
{
    Eq,
    Neq
}
public class Filter
{
    public string Name { get; set; }
    public string Value { get; set; }
    public Comparison Comparison { get; set; }
}

My Action should accept an Array of Filters

public async Task<IActionResult> Index([FromQuery(Name = "filter")]Filter[] filter)
{
    return Ok();
}

filter is always an empty array with this url

https://localhost:5001/configtemplates?filter=%7B%0A%20%20%22Name%22%3A%20%22Name%22%2C%0A%20%20%22Value%22%3A%20%22Test%22%2C%0A%20%20%22Comparison%22%3A%201%0A%7D&filter=%7B%0A%20%20%22name%22%3A%20%22Description%22%2C%0A%20%20%22value%22%3A%20%22Test%22%2C%0A%20%20%22comparison%22%3A%201%0A%7D.

When change the type of filter to string, it maps a string[]. therefore i expect the issue to be in the binding of Filter[].

How can i make it work bind to Filter[]?

UPDATE:

Filter[] can be parsed if the URL is in this format https://localhost:5001/configtemplates?filter%5B0%5D.n=Name&filter%5B0%5D.c=6&filter%5B0%5D.v=bla&filter%5B1%5D.n=Name&filter%5B1%5D.c=6&filter%5B1%5D.v=bli&filter%5B2%5D.n=ID&filter%5B2%5D.c=6&filter%5B2%5D.v=blu

which is OK for now but i am not sure if this is official standard. e.g. swagger does not work.

Thank you!

Marius Steinbach
  • 548
  • 7
  • 19

1 Answers1

0

You can use System.Text.Json to Deserialize it. Example link: https://localhost:5001/api/test/Index?filter=[{%22name%22:%22HalloWelt%22},{%22Name%22:%22bbbb%22}]

public async Task<IActionResult> Index([FromQuery(Name = "filter")]string filter)
{
    List<Filter> filters = System.Text.Json.JsonSerializer.Deserialize<List<Filter>>(filter);
    return Ok();
}