I’ve been having trouble trying to be able to have multiple “Get” methods while also having a default of /api/{controller}. Here is an example with dummy code (yes I realize it would return the exact same).
Error: "Multiple actions were found that match the request" when I try to go to /api/courses
Going to /api/courses/all works fine and so does /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 looks like:
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}"
);
What I want to be able to do is call:
/api/courses - returns Get()
/api/courses/all - returns GetAll()
/api/courses/3 - returns Get(id = 3)
Edit:
It also needs to allow for Put, Post, etc to work as well such as Putting a course on /api/courses/
By specifying the default action to be “Get” for the
ApiControllerOnlyroute, I was able to get/api/coursesto work. Does this work for your scenario?Edit:
If you have additional Put/Post/etc methods in you APIController, you should consider using
HttpMethodConstrainton theApiControllerOnlyroute and add another route (i.e.ApiControllerOnly2) for the Put/Post/etc methods: