Search code examples
asp.net-coreodata

.net Core 2.0 OData - EnableQuery attribute not allowing operations


I am trying to use Microsoft.AspNetCore.OData, v7.0.0.0-beta1, in a simple project. I am failing to filter, select, and use top or skip.

Overriding the ValidateQuery of the EnableQueryAttribute, I can successfully perform these type of operations so I believe the library is buggy. I could not find the right channel to report the issue, can anyone help?

The sample code to reproduce is available here: https://github.com/norcino/AspNetCoreSamples/tree/master/SampleODataApp


Solution

  • The solution to the problem was the actual initialization of the MVC route builder. Like in the .net framework version it is possible to specify which operation is allowed for OData query. In the example below I am whitelisting everything but you can do a more fine tuning passing an instance of QueryOptionSetting.

            app.UseMvc(routeBuilder =>
            {
                routeBuilder
                    .Select()
                    .Expand()
                    .Filter()
                    .OrderBy(QueryOptionSetting.Allowed)
                    .MaxTop(2000)
                    .Count();
                routeBuilder.EnableDependencyInjection();
            });
    

    Note that, the attribute [HttpGet, EnableQuery(AllowedOrderByProperties = "Id")], will effectively enforce the order by to the sole Id property specified in the attribute, but first you need to enable all from the configuration.