My controller action works fine with OData $select operation (without EnableQueryAttribute). However as I need a default pageSize to be specified I need to use the EnableQuery(PageSize=100) on my controller action. Once I do this OData throws and ODataException with message
The query specified in the URI is not valid. Could not find a property named 'PropertyA' on type 'Microsoft.AspNet.OData.Query.Expressions.SelectSomeAndInheritance_1OfPropertiesViewModel'.
This only happens when I use the EnableQueryAttribute.
(NB. I am aware that I can specify a pageSize with the ODataQueryOptions.ApplyTo (iQueryable, new ODataQuerySettings{PageSize=100} but this is not suitable as it causes the query to execute. ))
I am using version 7.2.2 of Microsoft.AspNetCore.OData
Hope this helps someone but in the end here is a workaround as EnableQueryAttribute simply does not work with $select. Firstly I removed the EnableQueryAtrribute completely. To achieve the default page size I needed I called ApplyTo without specifying the ODataQuerySettings(When you specify PageSize it incorrectly forces execution against the db server). Then using the IQueryable returned from ApplyTo I added the Take method dynamically like so StackOverflowAnswer. Kudos to Aleks Andreev
We seem to have the same problem after upgrading from 7.2.1 to 7.2.2. Reverting back to 7.2.1 seems to resolve this issue.
Our URL with problems looks like this with $select, $filter, and $orderby:
https://localhost:5001/api/v1/odata/MyModel?$select=rowUid,title&$filter=contains(title,%27%27)&$orderby=title
Like @ciranmc, our affected endpoints also have the [EnableQuery] attribute.
Exception stacktrace:
2019-10-16 17:42:36.9913|1|ERROR|Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware|An unhandled exception has occurred while executing the request. System.ArgumentNullException: Value cannot be null.
Parameter name: property
at System.Linq.Expressions.Expression.Property(Expression expression, PropertyInfo property)
at Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder.CreatePropertyValueExpressionWithFilter(IEdmStructuredType elementType, IEdmProperty property, Expression source, ExpandedReferenceSelectItem expandItem)
at Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder.BuildPropertyContainer(IEdmStructuredType elementType, Expression source, Dictionary`2 propertiesToExpand, ISet`1 propertiesToInclude, ISet`1 autoSelectedProperties, Boolean isSelectingOpenTypeSegments)
at Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder.ProjectElement(Expression source, SelectExpandClause selectExpandClause, IEdmStructuredType structuredType, IEdmNavigationSource navigationSource)
at Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder.GetProjectionLambda()
at Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder.Bind(IQueryable queryable)
at Microsoft.AspNet.OData.Query.ODataQueryOptions.ApplySelectExpand[T](T entity, ODataQuerySettings querySettings)
at Microsoft.AspNet.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)
at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)
at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext actionExecutedContext)
at Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
I have same problem using EnableQueryAttribute
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
for change query in server side,
this broken the SELECT query, is possibile to fix?
Anything happening here? $select does not work with EnableQueryAttribute in version 7.3.0.
It's serializing what i believe is the internal SelectExpandBinder<> in the results instead of the projection query results.
Anything happening here? $select does not work with EnableQueryAttribute in version 7.3.0.
It's serializing what i believe is the internalSelectExpandBinder<>in the results instead of the projection query results.
Update: I found that it works, but only when using Microsoft.AspNetCore.Mvc.NewtonsoftJson instead of System.Text.Json which is default with AspNetCore 3.1.
I guess you have different issues registered for supporting System.Text.Json and also EndpointRouting. I will search and subscribe to updates to these issues if I can find them.
I'm also getting the same issue @josundt
Any idea when this will be working for System.Text.Json ?
From my findings, latest version of Microsoft.AspNetCore.OData is unfortunately not yet compatible with the latest AspNetCore default stack. Fortunately, there is a way to fall back to previous implementations of:
I hope supporting the latest routing and serialization for AspNetCore apps is given the appropriate priority and that it will be working soon. (at least in .net 5)