0
votes

I have an api controller:

[RoutePrefix("api/users")]
[Authorize]
public class UsersController : ApiController

that has two Get methods:

[HttpGet]
[Route("")]
public async Task<HttpResponseMessage> Get(ODataQueryOptions<ApplicationUser> options)
{
        return Request.CreateResponse(HttpStatusCode.OK, new List<ApplicationUser>());
}

[HttpGet]
[Route("")]
public async Task<HttpResponseMessage> Get()
{
        return Request.CreateResponse(HttpStatusCode.OK, new List<ApplicationUser>());
}

Calling http://mysite/api/users?$filter=FirstName eq 'George' or Calling http://mysite/api/users

causes the exception Multiple actions were found that match the request.

Commenting out either method will cause the other to work.

Any help would be appreciated.

1

1 Answers

0
votes

All the Web API routing is about converting url into controller/action. And its mapping must be unambiguous.

In case that we would have only the first actionGet(ODataQueryOptions<ApplicationUser> options) it would match both urls below:

http://mysite/api/users?$filter=FirstName+eq+'George' 
http://mysite/api/users

The first url will be converted into call Get(someODataValue), the second could be Get(null)

The same could be applied to second method Get() without params, because both urls will be converted into parameterless call Get() (OData part will be skipped)

So the solution usually should be in two methods, which are really unique by params. E.g. one is object/refence the second is valueType/int

[HttpGet]
public async Task<HttpResponseMessage> Get(ODataQueryOptions<ApplicationUser> options)
{ ... }

[HttpGet]
public async Task<HttpResponseMessage> Get(int id)
{ ... }