【发布时间】:2015-05-13 11:00:46
【问题描述】:
我正在尝试找出在 REST 控制器中拥有多个 Get 操作的最佳方式。
我想做这样的事情:
按 ID 获取:
public ResponseType Get(Guid id)
{
// implementation
}
通过枚举类型获取:
public ResponseType Get(EnumType type)
{
// implementation
}
通过其他枚举类型获取:
public ResponseType Get(OtherEnumType otherType)
{
// implementation
}
等等。
现在,当我执行类似操作时,会收到下一条错误消息:
Multiple actions were found that match the request
我明白为什么我会收到这条消息,并且我在想最好的方法是如何做这样的事情(我想坚持使用 REST)。
我知道我可以添加这样的路线:
routeTemplate: "api/{controller}/{action}/{id}"
但是我需要更改动作名称和网址 - 当我们谈论休息时,这似乎是一种解决方法。
我想到的另一件事是用一个 Get 创建多个控制器 - 但这似乎更错误。
第三种解决方法是使用具有以下状态的输入参数处理一个 Get 操作:
public ResponseType Get(ReqeustObj obj)
{
switch(obj.RequestType)
{
case RequestType.GetById:
// etc...
}
}
无论如何,我想知道在 REST (WebApi) 中执行类似操作的最佳方法是什么。
【问题讨论】:
标签: asp.net rest asp.net-web-api