【发布时间】:2013-06-23 00:31:24
【问题描述】:
我将 OData 与 Web api 一起使用,并尝试根据类型细节进行查询。我想要的是提供一个看起来像这样的查询(粗体文本在语法上不正确,但描述了我在断言类型后要做什么):
../Supplier?$filter=isof(Product, namespace.ToyProduct)&Product.ratedAge eq 5
但是,当我尝试使用 [Queryable] 属性并从 ODataController 继承时,我得到一个关于 isof 的 method not supported error。有什么方法可以支持该方法吗?我尝试将 AllowedFunctions.All 添加到 Queryable 属性,但这没有用。
我可以使用 ODataQueryOptions 检查过滤器中是否包含 isof 并使用我自己的方法进行检查,但是它相当混乱,并且在允许额外过滤后可能会变得更糟,如果存在更好的解决方案那就太棒了!
当前未编译的代码:
public class SupplierController : ODataController {
[Queryable(AllowedFunctions = AllowedFunctions.All)]
public PageResult<Supplier> Get(ODataQueryOptions<Supplier> queryOptions) {
// doesn't work, method not supported
//var queryableSuppliers = queryOptions.ApplyTo(_repository.All());
// works but messy method.
var queryableSuppliers = MyIsOfMethod(queryOptions.Filter)
return new PageResult<ObjectType>(
results as IEnumerable<ObjectType>,
Request.GetNextPageLink(),
Request.GetInlineCount());
}
}
【问题讨论】:
-
我有一个解决方案可以将
OfType调用发送到查询以模拟isof函数。代码见github.com/schungx/WebApi-OData。
标签: .net asp.net-web-api odata