bugnuker
bugnuker

Reputation: 3968

Web API Pagination and controller route - GetAll with pageNumber vs GetByID conflict

I'm creating an API that needs to have the the following ability: (Example only)

GetALLProducts(int pageNumber) - with paging (page size is static)

Then, I need the other route for Getting a Product by ID

GetProduct(int productID)

the problem here is this is the same signature, so the route that should work for getting a product by id is also the same route that gets all products but with a pagenumber. The route that gets executed is the GetProduct.

Example: www.url.com/api/Products/2 - Does this get the second page of all products? or gets product id number 2?

I thought about adding pageSize to the signature, but I want to make this a system static value.

What do you think is the best resolution and clean solution here? I saw this question: WebApi Multiple actions were found with GetAll() and GetByIds(int[] ids) and this could work as well. Thoughts?

Thanks!!

Upvotes: 1

Views: 997

Answers (1)

Dave Russell
Dave Russell

Reputation: 139

Pass pagenumber as a query string. You should separate call that will be routed accordingly - by convention /products?page=1 and /product/2, respectively. Your specified route will be able to distinguish the two. Oh and my preference is to have attributed routing as suggested in the thread.

Upvotes: 3

Related Questions