문제

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.

도움이 되었습니까?
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top