如何使用BreezeJs在Web API MVC 4中进行分页

时间:2012-10-18 18:23:38

标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api breeze

我有一个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而不是总计。

1 个答案:

答案 0 :(得分:3)

很好的问题。当前版本的Breeze中的OData支持不理解$inlinecount parameter for OData 2.0,我认为这是你正在寻找的。这是积压的。请注明idea on the Breeze user voice