I am new to ASP.Net WebAPI and I'm trying to use the NorthWind database to practice.
Now, I have a ProductsController
which contains a GetAllProducts
and a GetAllProductsById
actions.
On the client-side, I have a multiselect dropdown control which is been populated with the categories of products from the CategoriesController
class.
I want to populate a second dropdown control (not a multiselect) with the Products
from the Categories
that was selected in the category dropdown list.
My GetAllProductsById
controller looks like this:
public IHttpActionResult GetAllProductsById([FromUri]int[] id)
{
using (var ctx = new NorthwindEntities())
{
<some codes...>
}
return Ok(productList);
}
Calling this service from the client, the URL look like this: http://localhost:1234/api/Products/GetAllProductsById?id=1&id=2&id=3
This is looks good with few parameters but what if the User selects more categories (let's say 30 out of 40 categories)? This means the URL would be so long.
So, I decide to use a POST
to do a GET
's job by decorating my action with HttpPost
:
[HttpPost]
public IHttpActionResult GetAllProductsById([FromBody]int[] id)
This allows me to send the id
parameters from the Body of my request.
Now, I am wondering if this style is correct or if someone can point me to a much cleaner way of passing a long list of parameters from a client to my webservice.
Thanks in advance.
NB:
- Using Elasticsearch is not an option at the as suggested in the link below: HTTP GET with request body
- I tried using a modal class but it also has the same effect
http://localhost:1234/api/Products/GetAllProductsById?input.id=1&input.id=2