[英]$inlinecount not working on ApiController using ApplyTo
I'm trying to use the OData filtering and paging capabilities in a standard Web API 2.2 ApiController
. 我正在尝试在标准Web API 2.2 ApiController
使用OData过滤和分页功能。 For this, I have to rewrite the request URL to conform to the OData v4 standards. 为此,我必须重写请求URL以符合OData v4标准。 My controller looks like this: 我的控制器如下所示:
public GridPage Get([FromUri] GridSearchCriteria criteria)
{
Request.RequestUri = ... // convert querystring to OData v4
var context = new ODataQueryContext(MyEdmModel.Instance, typeof(Delivery), null);
ODataQueryOptions<Delivery> options = new ODataQueryOptions<Delivery>(context, Request);
IQueryable<Delivery> deliveries = ... // use EF to load deliveries from DB
var result = (IQueryable<Delivery>)options.ApplyTo(deliveries); // BTW, I wonder why there is no generic overload of ApplyTo?
// fill and return a GridPage
...
}
So far everything works nice as expected. 到目前为止,一切正常。
Now, I'm interested in the total count of the filtered items and thus I've added $inlinecount=allpages
to the query string. 现在,我对筛选出的项的总数感兴趣,因此,我在查询字符串中添加了$inlinecount=allpages
。 The resulting request URI looks like this: 结果请求URI如下所示:
http://localhost:54026/.../deliveries/page?$top=10&$skip=0&$inlinecount=allpages}
Then, I'm trying to retrieve the total count like this (after the call to ApplyTo
): 然后,我正在尝试像这样(调用ApplyTo
)检索总计数:
long? totalCount = Request.ODataProperties().TotalCount;
Unfortunately, totalCount
always stays null no matter what I try. 不幸的是,无论我尝试什么, totalCount
始终保持为空。 I've also tried using: 我也尝试过使用:
object totalCount;
Request.Properties.TryGetValue("System.Web.OData.TotalCount", out totalCount);
but no luck. 但没有运气。 A peek into the Request
properties reveals that there is an entry under System.Web.OData.Properties
but all its properties are uninitialized ( Model
is null, NextLink
is null, TotalCount
is null, etc.). 窥视Request
属性会发现System.Web.OData.Properties
下有一个条目,但是其所有属性都未初始化( Model
为null, NextLink
为null, TotalCount
为null,等等)。
Does anybody have an idea why this isn't working? 有人知道为什么这行不通吗? BTW, I'm using Microsoft.AspNet.OData
v5.6. 顺便说一句,我正在使用Microsoft.AspNet.OData
v5.6。
I had to specify $count=true
instead of $inlinecount=allpages
. 我必须指定$count=true
而不是$inlinecount=allpages
。 According to https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/ this was a breaking change somewhere. 根据https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/的描述,这是某处的重大变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.