我正在使用Microsoft.AspNet.OData v6.0.0
并期望将MaxTop
值设置为10
将启用$top
查询选项.
但是,请求URL http://localhost:23344/odata/v4/Resources?$top=10
仍然给我错误:
{"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)"}}}
好像顶部查询的限制仍为0.
public class ResourcesController : ODataController { private IResourceService resourceService; public ResourcesController(IResourceService resourceService) { this.resourceService = resourceService; } [EnableQuery(MaxTop=10)] public IQueryableGet() { return resourceService.GetResources().AsQueryable(); } [EnableQuery] public SingleResult Get([FromODataUri] int key) { var result = resourceService.GetResources().Where(r => r.Id == key).AsQueryable(); return SingleResult.Create(result); } }
public static void Register(HttpConfiguration config) { ODataConventionModelBuilder builder = new ODataConventionModelBuilder { Namespace = "MyNamespace", ContainerName = "DefaultContainer" }; builder.EntitySet("Resources"); builder.EntityType ().Select().Count().Expand().OrderBy(); config.MapODataServiceRoute( routeName: "ODataRoute", routePrefix: "odata/v4", model: builder.GetEdmModel()); }
Github问题描述了MaxTop行为的可能错误
我启用的每个其他查询选项,包括$skip
.
如在此问题设置config.Select().Expand().Filter().OrderBy().MaxTop(null).Count();
在WebApiConfig.cs
之前config.mapODataServiceRoute(...
.没工作.
像在同一个问题中一样添加[Page(MaxTop = 100)]
到我的Resource
模型中.没工作.
[Page]
在模型上设置属性.从WebApi OData文档 "如果您设置页面属性,默认情况下它将启用$ top,没有限制最大值".没工作.
[EnableQuery(PageSize=10)]
在控制器上设置属性.从WebApi OData文档 "如果您设置页面属性,默认情况下它将启用$ top,没有限制最大值".启用分页但无效.
该错误表示每种情况下顶部的限制为0
好的,我想我明白了.
我有同样的问题,并发现如果你在构建器中指定任何规则,就像你做的那样(我做了):
builder.EntitySet
按属性设置的值将被覆盖并设置为0.
如果你删除这些条目并将全局配置放在一起
config.Select().Expand().Filter().OrderBy().MaxTop(600).Count();
就行了.
您也可以使用构建器上的流畅界面来定义MaxTop.
builder.EntityType
即使您在构建器中为实体类型定义其他规则,它也会起作用.
总结: 当您在Fluent构建器界面中定义某个配置并且无法使用属性(控制器上的EnableQuery和模型上的Page)时,可能会导致创建新配置.可能这是一个错误,我将在odata存储库中将其报告为问题.因为他们仍然推荐属性方法.