I'm using Microsoft.AspNet.OData v6.0.0
and expect that setting the MaxTop
value to 10
will enable the $top
query option.
However, requesting the URL http://localhost:23344/odata/v4/Resources?$top=10
still gives me the error:
{"error":{"code":"","message":"The query specified in the URI is not valid. The limit of '0' for Top query has been exceeded. The value from the incoming request is '10'.","innererror":{"message":"The limit of '0' for Top query has been exceeded. The value from the incoming request is '10'.","type":"Microsoft.OData.ODataException","stacktrace":" at System.Web.OData.Query.Validators.TopQueryValidator.Validate(TopQueryOption topQueryOption, ODataValidationSettings validationSettings)\r\n at System.Web.OData.Query.TopQueryOption.Validate(ODataValidationSettings validationSettings)\r\n at System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor, ODataQueryContext queryContext)\r\n at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"}}}
As though the top query still has a limit of 0.
Controller
public class ResourcesController : ODataController
{
private IResourceService resourceService;
public ResourcesController(IResourceService resourceService)
{
this.resourceService = resourceService;
}
[EnableQuery(MaxTop=10)]
public IQueryable<Resource> Get()
{
return resourceService.GetResources().AsQueryable();
}
[EnableQuery]
public SingleResult<Resource> Get([FromODataUri] int key)
{
var result = resourceService.GetResources().Where(r => r.Id == key).AsQueryable();
return SingleResult.Create(result);
}
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder
{
Namespace = "MyNamespace",
ContainerName = "DefaultContainer"
};
builder.EntitySet<Resource>("Resources");
builder.EntityType<Resource>().Select().Count().Expand().OrderBy();
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata/v4",
model: builder.GetEdmModel());
}
What I've found
Github issue describing possible bug with behaviour of MaxTop
What does work
Every other query option I've enabled, including $skip
.
What I've Tried
As in this question Setting config.Select().Expand().Filter().OrderBy().MaxTop(null).Count();
in the WebApiConfig.cs
before config.mapODataServiceRoute(...
. Didn't work.
Adding [Page(MaxTop = 100)]
to my Resource
model as in the same question. Didn't work.
Setting [Page]
attribute on model. From WebApi OData documentation "if you set the Page Attribute, by default it will enable the $top with no-limit maximum value". Didn't work.
Setting [EnableQuery(PageSize=10)]
attribute on controller. From WebApi OData documentation "if you set the Page Attribute, by default it will enable the $top with no-limit maximum value". Enabled paging but Didn't work.
The error says the limit was 0 for top in every case