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.