我有一个 WebApi 控制器,并添加了来自 BreezeJs 的过滤器以支持可查询的请求,例如 $orderby=DateAdded&$top=8。
GlobalConfiguration.Configuration.Filters.Add(
new Breeze.WebApi.ODataActionFilter());
一切正常。这只给出8个结果。我需要做些什么来添加这样的总计数也与结果一起返回?
更新
public class ODataHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken).ContinueWith(
task =>
{
var response = task.Result;
if (ResponseIsValid(response) && ShouldInlineCount(request))
{
object responseObject;
response.TryGetContentValue(out responseObject);
if (responseObject is IQueryable)
{
var renum = responseObject as IEnumerable<object>;
if (renum != null)
{
response = request.CreateResponse(HttpStatusCode.OK, new ODataMetadata<object>(renum, renum.Count()));
}
}
}
return response;
});
}
private bool ShouldInlineCount(HttpRequestMessage request)
{
var queryParams = request.RequestUri.ParseQueryString();
var inlinecount = queryParams["$inlinecount"];
return string.Compare(inlinecount, "allpages", true) == 0;
}
private bool ResponseIsValid(HttpResponseMessage response)
{
if (response == null || response.StatusCode != HttpStatusCode.OK || !(response.Content is ObjectContent)) return false;
return true;
}
}
这是我到目前为止想出的。但问题是过滤器在处理程序启动之前已经将 TOP 8 添加到请求中。所以计数将是 8,而不是总数。