我一直在尝试能够拥有多个“Get”方法同时还拥有默认的 /api/{controller} 时遇到了麻烦。这是一个带有虚拟代码的示例(是的,我意识到它会返回完全相同的)。
错误:"Multiple actions were found that match the request"
当我尝试去/api/courses
去/api/courses/all
工作正常,所以做/api/courses/3
// GET api/courses
public IEnumerable<Courses> Get()
{
return Db.Courses.OrderBy(x => x.Name);
}
// GET api/courses/all
[ActionName("all")]
public IEnumerable<Courses> GetAll()
{
return Db.Courses.OrderBy(x => x.Name);
}
// GET api/courses/id
[ActionName("all")]
public Courses Get(int id)
{
return Db.Courses.Where(x => x.id == id);
}
RouteConfig
好像:
routes.MapHttpRoute(
"ApiControllerOnly",
"api/{controller}"
);
// Allow for numeric Ids to be passed in
routes.MapHttpRoute(
"ApiControllerAndIntegerId",
"api/{controller}/{id}",
null,
new { id = @"^\d+$" }
);
routes.MapHttpRoute(
"ApiControllerAction",
"api/{controller}/{action}"
);
我想要做的是调用:
/api/courses - returns Get()
/api/courses/all - returns GetAll()
/api/courses/3 - returns Get(id = 3)
编辑:
它还需要允许 Put、Post 等工作,例如Putting
/api/courses/ 上的课程