无法使用oData查询选项

时间:2012-08-28 14:45:48

标签: c# asp.net-web-api odata asp.net-web-api-odata

我有一个ASP.NET Web API项目。我正试图将一些查询选项传递给我的API控制器,如下所示:

http://localhost:61736/api/Enquiries?
callback=callback&$top=30&$skip=30&orderby=EnquiryId
&$inlinecount=allpages&_=1346164698393

但我得到以下内容:

The query parameter '$inlinecount' is not supported.

当我尝试使用$callback$format

时,我也会这样做

知道我做错了什么吗?根据:http://msdn.microsoft.com/en-us/library/ff478141.aspx我应该可以使用它们吗?

4 个答案:

答案 0 :(得分:7)

ASP.NET Web API仅提供有限对OData的支持documented in this blog post.我没有看到您在该列表中提到的查询参数。

答案 1 :(得分:2)

在当前版本中,web api仅支持$ filter,$ orderby,$ top和$ skip。您可以覆盖QueryableAttribute以在OData协议上添加更多支持。公共nuget发布后的checkin使ValidateQuery方法变为虚拟,以便您可以覆盖它以绕过验证。请在http://www.myget.org/F/aspnetwebstacknightly/尝试我们的夜间构建。

您也可以使用ODataQueryOptions。以下代码等同于[Queryable]属性,但它在看到不支持的选项时不会抛出异常。

public IEnumerable<Product> Get(ODataQueryOptions options) 
{
    return options.ApplyTo(_db.Products as IQueryable) as IEnumerable<Product>; 
}

你可以通过ODataQueryOptions.RawValues.InlineCount获得$ inlinecount。 有关OData查询支持的详细信息,请参阅:http://blogs.msdn.com/b/alexj/archive/2012/08/21/web-api-queryable-current-support-and-tentative-roadmap.aspx

答案 2 :(得分:2)

12月12日在项目中检查了对$ inlinecount的支持,可能是下一个版本将包含此支持。你可以下载最新的资源或者选择每晚建设:

http://aspnetwebstack.codeplex.com/SourceControl/changeset/ed65e90e83c8

Revision: ed65e90e83c8f9391b4f4806d305c83f55d28ff6
Author: youssefm < youssefm@microsoft.com >
Date: 12/6/2012 1:51:44 PM
Message:
[OData] Add support for the $inlinecount query option

我相信每晚的套餐会被推送到http://www.myget.org/F/aspnetwebstacknightly/,但我还没有验证自己。

答案 3 :(得分:1)

如果你有任何机会使用KendoUI this post解释了如何通过切换到JSON而不是JSONP来禁用某些选项,例如$ callback。