0

We have recently added support for operators in query params e.g.:

/api/books?price[gt]=10

/api/books?price[eq]=10 or /api/books?price=10

...

The previous behaviour only allowed:

/api/books?price=10

To achieve the above filtering capability we added a dictionary field to the request model:

public class BookRequestModel
{
 
    [FromQuery]
    public int Price { get; set; }

 
    [FromQuery(Name = "Price")]
    public Dictionary<string, int> PriceOperators { get; set; }

}

Now, this works fine but it requires us to add two query param properties for each query param. Now is it possible to dynamically update the query param key before model binding and append "[eq]". For example, if user makes the following request:

/api/books?price=10

It gets updated to

/api/books?price[eq]=10

before model binding step. This way we only need one property in the request model for each query param:

public class BookRequestModel
{
 
    [FromQuery]
    public Dictionary<string, int> Price { get; set; }

}

I tried changing the query param key in middleware but context.Request.Query is readonly and doesn't allow updating the keys.

adnan kamili
  • 8,967
  • 7
  • 65
  • 125
  • Why are you reinventing GraphQL, but poorly? – Ian Kemp Aug 23 '21 at 11:02
  • https://stackoverflow.com/questions/29320452/binding-querystring-values-to-a-dictionary – Asad Aug 23 '21 at 11:05
  • Use this link and try capital P for price – Asad Aug 23 '21 at 11:05
  • Aside from GraphQL, OData is another standardized way of querying. Of course you can always roll your own, but as needs become more complicated you may find moving to a full, already implemented query language in advance beneficial anyway, before you're left maintaining your very own in perpetuity. – Jeroen Mostert Aug 23 '21 at 13:53

0 Answers0