tcarvin
tcarvin

Reputation: 10855

How to disambiguate between two Actions in ApiController

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.

Upvotes: 1

Views: 200

Answers (1)

Kiran
Kiran

Reputation: 57999

This is a known issue with action selection.

http://aspnetwebstack.codeplex.com/workitem/821

http://aspnetwebstack.codeplex.com/workitem/322

Upvotes: 3

Related Questions