我正在使用Microsoft.AspNet.OData v6.0.0
并期望将MaxTop
值设置为10
将启用$top
查询选项。
但是,请求网址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 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);
}
}
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());
}
Github issue describing possible bug with behaviour of MaxTop
我已启用其他所有查询选项,包括$skip
。
与config.Select().Expand().Filter().OrderBy().MaxTop(null).Count();
之前WebApiConfig.cs
中的config.mapODataServiceRoute(...
设置[Page(MaxTop = 100)]
一样。 无效。
将Resource
添加到我的[Page]
模型,就像在同一个问题中一样。 无效。
在模型上设置[EnableQuery(PageSize=10)]
属性。如果您设置了页面属性,则从this question“开始,默认情况下,它将启用$ top且无限制最大值”。 没有工作。
在控制器上设置{{1}}属性。如果您设置了页面属性,则从WebApi OData documentation“开始,默认情况下,它将启用$ top且无限制最大值”。 启用分页但无法正常工作。
错误表示每种情况下限制为0
答案 0 :(得分:5)
好的,我想我明白了。
我遇到了同样的问题并发现如果你在构建器中指定任何规则就像你做的那样(我做了):
builder.EntitySet<Resource>("Resources");
builder.EntityType<Resource>().Select().Count().Expand().OrderBy();
按属性设置的值将被覆盖并设置为0.
如果您删除这些条目并将全局配置设置为
config.Select().Expand().Filter().OrderBy().MaxTop(600).Count();
它会工作。
此外,您可以使用构建器上的流畅界面来定义MaxTop。
builder.EntityType<T>().Page(100, 100);
即使您在构建器中为实体类型定义其他规则,它也会起作用。
<强>总结强> 当您在Fluent构建器界面中定义某个配置并且无法使用属性(控制器上的EnableQuery和模型上的Page)时,可能会导致创建新配置。 可能它是一个错误,我会在odata存储库上将其报告为问题。因为他们仍然推荐属性方法。
答案 1 :(得分:0)
我有同样的错误。在我的情况下,我尝试隐藏Select(..)
上EntityType<..>()
扩展名的实体中的特定字段。
builder
.EntityType<User>()
.Select(System.Web.OData.Query.SelectExpandType.Disabled, "Password");
这发生在我的GetModel()
- 方法中。所以我只是改变了设置全局设置的顺序:
config.Count().Filter().OrderBy().Expand().Select().MaxTop(100);
IEdmModel model;
model = GetModel();