当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - 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!!

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: