Open norcino opened 5 years ago
I had to remove the routing tag from the controller.
[Route("{tenant}/odata/[controller]")]
Turns out that this messes completely OData, and in fact having the rout specified in the controller, I get exception when I add another get method, for example to Get all and Get(int key) by Id.
In this case the error is:
An unhandled exception occurred while processing the request. AmbiguousMatchException: The request matched multiple endpoints. Matches:
WebApplication1.Controllers.ValuesController.Get (WebApplication1) WebApplication1.Controllers.ValuesController.Get (WebApplication1) Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateSet candidates)
I am not closing this issue, because I tried to change the base controller from ODataController to ControllerBase, and with (or without) the ApiControllerAttribute I still get the same behaviour. I though OData was supposed to be easily retrofit to an existing controller, so I think the RouteAttribute should not clash in this way with the OData routing convention.
An additional issue I am trying to solve, is that if I remove the RouteAttribute, the IHttpContextAccessor does not have anymore the RouteData in the request context, which is something I used so far to take the tenant name from the routed url for the Authorization.
Setting up OData route using MapODataServiceRoute causes the controller to block all operations such, selection, filtering and ordering. I tried to use SetDefaultQuerySettings enabling everything, or whitelisting everything in the EnableQuery attribute but with no success. The only way I could solve my problem was to use app.UseOData to register odata route.
Assemblies affected
Microsoft.AspNetCore.App 2.2.0 Microsoft.AspNetCore.OData 7.2.1
Reproduce steps
Create an AspNet core 2.2 application, and register the OdataRoute using:
Startup.cs
AssessmentsController.cs
Expected result
I would expect to be able to filter, select, order and so on.
Actual result
Selection, filtering and ordering requests are not accepted:
Additional detail
The order by is accepted when I use in the EnableQueryAttribute AllowedOrderByProperties = "PropertyName". Then to fix the issue I had to change the way I register the OData route, as shown below.
app.UseOData("odata", "{tenant}/odata", model);
Maybe I am doing something wrong, but if find it very misleading, so it is possible that this is a bug.
UPDATE 1 Using routeBuilder.Select().Filter(); in UseMvc also allows me to enable filtering selection and so on. But if I enable in this way I cannot restrict filtering or allow filtering in detail using the model.
So to clarify:
used in combination with:
routeBuilder.Select().Filter();
Allows me to perform filtering on the property "AssessmentId" even if I try to disable it from the model, so it seems that either is ignored (as I think) or the setup done on the route builder has higher priority then the settings in the model.