Ideally I would like to have an URL in following format:
/api/categories/1,2,3...N/products
And this would return all products for the specified categories. Having one API call with multiple category IDs saves me several database calls, thus improves performance.
I can easily implement this in a following way.
public HttpResponseMessage GetProducts(string categoryIdsCsv)
{
// <1> Split and parse categoryIdsCsv
// <2> Get products
}
However, this doesn't look like a clean clean solution, and possibly breaking SRP principle. I also tried using ModelBinder, however it adds parameters to query string.
Questions:
- Is there a clean way to implement such URL structure?
- Or is there a different/better approach to retrieve all products for multiple categories?
Please let me know if you need any further clarification.
/api/products?categories[]=1&categories[]=2&...? This is kind of the standard way such things are done. Also, Web API 2 parses those query parameters into the controller method parameterint[] categoriesautomatically, so there is no need for a customModelBinder.