$ inlinecount不使用ApplyTo上ApiController工作 [英] $inlinecount not working on ApiController using ApplyTo
问题描述
我想在一个标准的Web API 2.2使用OData的筛选和分页功能 ApiController
。对于这一点,我不得不重写请求URL以符合OData的V4标准。我的控制器看起来是这样的:
I'm trying to use the OData filtering and paging capabilities in a standard Web API 2.2 ApiController
. For this, I have to rewrite the request URL to conform to the OData v4 standards. 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.
现在,我很感兴趣,经过滤项的总数,因此我添加了 $ inlinecount =所有页
来查询字符串。由此产生的请求URI是这样的:
Now, I'm interested in the total count of the filtered items and thus I've added $inlinecount=allpages
to the query string. The resulting request URI looks like this:
<$c$c>http://localhost:54026/.../deliveries/page?$top=10&$skip=0&$inlinecount=allpages}$c$c>
然后,我试图找回这样的总计数(调用后 ApplyTo
)
Then, I'm trying to retrieve the total count like this (after the call to ApplyTo
):
long? totalCount = Request.ODataProperties().TotalCount;
不幸的是, TOTALCOUNT
始终保持为空,不管我怎么努力。我使用也尝试:
Unfortunately, totalCount
always stays null no matter what I try. I've also tried using:
object totalCount;
Request.Properties.TryGetValue("System.Web.OData.TotalCount", out totalCount);
但没有运气。具有浓郁的请求
属性表明,有在 System.Web.OData.Properties
的条目,但它的所有属性未初始化(模式
为空, NEXTLINK
为空, 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.).
没有任何人有一个想法,为什么这是不工作?顺便说一句,我使用 Microsoft.AspNet.OData code> V5.6。
Does anybody have an idea why this isn't working? BTW, I'm using Microsoft.AspNet.OData
v5.6.
推荐答案
我必须指定 $数= TRUE
而不是 $ inlinecount =所有页
。据<一个href=\"https://damienbod.word$p$pss.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/\" rel=\"nofollow\">https://damienbod.word$p$pss.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/这是一个重大更改的地方。
I had to specify $count=true
instead of $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.
这篇关于$ inlinecount不使用ApplyTo上ApiController工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!