该请求与 ASP.Net Core OData 中的多个端点匹配

The request matched multiple endpoints in ASP.Net Core OData

我完成了第一个 OData-Request 工作:

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

现在我想为 Get(id) 添加第二个:

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

现在的问题是,当我尝试用邮递员执行第一次调用时,结果是: GET http://localhost:5000/api/v1/applicationuser?$filter=startsWith(LastName,%20%27Test%27)

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:请求匹配了多个端点。匹配项:

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)

所以我不知道问题出在哪里。方法不同,第二个方法应该使用 ID 参数明确调用它。 我做错了什么?

问题是,我们需要使用类似 ORouteData-Attribute 的东西。但是在版本 8.0.0-rc 中这个属性没有了。

文档说,我们需要使用 Http 动词属性:

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