Search code examples
c#asp.net-coreodata.net-5

The request matched multiple endpoints in ASP.Net Core OData


I made my first OData-Request work:

[HttpGet]
[EnableQuery]
public IQueryable<ApplicationUser> Get()
{
    return _applicationUserRepository.GetAll();
}

now I wanted to add the second one for Get(id):

[HttpGet]
[EnableQuery]
public ApplicationUser Get([FromODataUri]long id)
{
    return _applicationUserRepository.Get(id).Result;
}

Problem now is, when I try to execute the first call with postman, the result is: GET http://localhost:5000/api/v1/applicationuser?$filter=startsWith(LastName,%20%27Test%27)

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:

  • MyAssambly.Controllers.ApplicationUserController.Get (MyAssambly)
  • MyAssambly.Controllers.ApplicationUserController.Get (MyAssambly)

at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] candidateState) at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.SelectAsync(HttpContext httpContext, CandidateSet candidateSet) at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g__AwaitMatch|8_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask) at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

So I don't have any idea what the problem could be. The methods are different and the second one should definitively call that with the ID parameter. What am I doing wrong?


Solution

  • The problem is, that we need to use something like the ORouteData-Attribute. But in Version 8.0.0-rc this attribute is gone.

    Documentation says, we need to use the Http verb attributes:

    [EnableQuery]
    [HttpGet("ApplicationUser")]
    [HttpGet("ApplicationUser/$count")]
    public IQueryable<ApplicationUser> Get()
    {
        return _applicationUserRepository.GetAll();
    }