API中的属性限制OData

API中的属性限制OData

本文介绍了通过Web API中的属性限制OData $ filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在OData查询$filter选项中使用某些属性.

I want to only allow some properties to be used in OData query $filter option.

我看到EnableQueryAttribute有一个AllowedOrderByProperties参数,但是我没有为$ filter找到另一个参数.我只是想念它吗?如果没有,那么将需要什么来实现呢?

I see that there an AllowedOrderByProperties parameter to EnableQueryAttribute, but I didn't find another for $filter. Did I just miss it? If not, what would it take to implement it?

推荐答案

您可以尝试以下操作:

一旦有了构建器,就可以列出entitySet的属性,然后可以提及该字段是否可过滤.

once you have the builder, you can list the properties of the entitySet then you can mention if the field is filterable or not.

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

并在控制器操作(例如httpGet)中添加

and in the controller action (the httpGet for example) add

options.Filter.Validate(allowedOptions);

如果字段不可过滤,则会抛出异常.

in case a field is not filterable this would throw an exception.

这篇关于通过Web API中的属性限制OData $ filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:08