This is a known issue with action selection.
How to disambiguate between two Actions in ApiController
-
06-03-2022 - |
문제
I'm having problems creating a route or other configuration that will disambiguate two Get methods. Here's a sample class:
public class UsersController : ApiController
public User[] GetMany([FromUri]int[] id)
{
// returns all users requested by id
}
public User[] GetAll()
{
// returns all users
}
}
I'd like myhost/api/users
to map to GetAll
, and myhost/api/users?id=123
to map to GetMany
.
Right now they both yield a 500 error because both methods are matched as possible actions for both URIs.
Here's my route:
routes.MapHttpRoute(
name: "AllUsersRoute",
routeTemplate: "api/users",
defaults: new { },
constraints: new { }
);
While I know in this simple example, the GetMany
method could be changed to treat an empty id list as a request for all, but in more complex scenarios this might not be the case.
Note, I'm using MVC 4 Web API, Visual Studio 2010.
해결책
제휴하지 않습니다 StackOverflow