A picture is worth a thousand words - as you can see in the following screenshots, I'm not able to retrieve the parameters in the query string and bind them to the ApplyFilter
model. I've tried [BindQuery]
, [BindProperty]
and [Bind]
, but unfortunately no luck at all. I keep getting null values on the filter parameter even though the parameters are passed to the QueryString
.
https://localhost:7061/getallproducts?withproductoptions=true&categoryid=1&pricefrom=1&priceto=30&vendorname=ArtWithLight
Code:
public class ProductsController : ControllerBase
{
private readonly IProductRepositery _prodRepo;
public ProductsController(IProductRepositery prodRepo)
{
_prodRepo = prodRepo;
}
[HttpGet]
public async Task<ActionResult<ProductDto>> GetAllProducts([FromQuery]ApplyFilter? filters)
{
return Ok(await _prodRepo.GetAllProductsAsync(filters));
}
}
public class ApplyFilter
{
public ApplyFilter()
{
}
[FromQuery(Name = "categoryid")]
List<int>? categoryIds { get; set; }
[FromQuery(Name = "withproductoptions")]
bool? withProductOptions { get; set; }
[FromQuery(Name = "pricefrom")]
decimal? priceFrom { get; set; }
[FromQuery(Name = "priceto")]
decimal? priceTo { get; set; }
[FromQuery(Name = "vendorname")]
string? vendorName { get; set; }
}
See screenshot: https://i.stack.imgur.com/qemMK.png
Thanks in advance.
Give this a go, have updated ApplyFilter() to use public properties. Check out the ASP.NET Core 6.0 Model Binding section of the docs which explains how it works.
Request example: