我只想允许在 OData 查询 $filter
选项中使用某些属性。
我看到 EnableQueryAttribute
有一个 AllowedOrderByProperties
参数,但我没有为 $filter 找到另一个参数。我刚刚错过了吗?如果没有,实现它需要什么?
最佳答案
您可以尝试以下方法:
一旦有了构建器,就可以列出 entitySet 的属性,然后可以提及该字段是否可过滤。
var entityTypeConfig = builder.EntitySet<SomeType>("SomeType").EntityType;
entityTypeConfig.Property(x => x.SomeField);
entityTypeConfig.Property(x => x.SomeField2).IsNotFilterable().IsNonFilterable();
// not sure what is the difference between them
并在 Controller 操作(例如 httpGet)中添加
options.Filter.Validate(allowedOptions);
如果某个字段不可过滤,则会抛出异常。
关于c# - 通过 Web API 中的属性限制 OData $filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040901/