How to work with ASP.Net WebApi overloaded methods?

*爱你&永不变心* 提交于 2019-12-05 10:47:49

Web API uses something called IHttpActionSelector to determine which action to invoke in the controller. I think the default implementation does not support overloads for scenarios like this. You can implement a custom IHttpActionSelector, but it might a lot of work. Take a look at the samples in this blog post,

http://www.novanet.no/blog/aanund-austrheim/dates/2012/7/several-post-methods-on-an-apicontroller-using-an-actionselector/

It would be much simpler to support a single scenario where you always receive a list of items. For the case of one item, it's just a list with a single item.

I suspect that the routing doesn't take the body into account. In your case, the only way to determine which overload to use is to parse the body and route appropriately. One simplification you could do is to always take a list of Item with a single method. Then in the case of a single item, just include that one item in the list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!