implementing oData inlinecount using asp.net webapi [duplicate]

試著忘記壹切 提交于 2019-12-21 23:35:25

问题


Possible Duplicate:
OData pagination with WebApi ( $inlinecount )

Since Asp.net WebAPi almost supports odata, its very enticing for me to get $inlinecount to work so that it plays nicely with kendo ui ( or any other). So that it returns value in jsonp format, i implemented a new MediaFormatter ( from Stackoverflow).

Trouble is that it needs the results to have count element in them in order to get server side paging to work, so for now i hacked the formatter to get a fake count to work. This all works great and the grid is all happy, however getting the real count is a challenge since IQueryable expression being returned already has filters/Take etc applied to it.

 public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext transportContext)
    {
        string callback;
        if (IsJsonpCountableRequest(out callback))
        {
            return Task.Factory.StartNew(() =>
            {
                var q = value as IQueryable<Movie>;
                var count = q.Count(); // this count doesnt return the actual count
                var writer = new StreamWriter(stream);
                writer.Write(callback + "({");
                writer.Write(@"""d""");
                writer.Write(" : { ");
                writer.Write(@"""results""");
                writer.Write(" : ");
                writer.Flush();
                base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext).Wait();
                writer.Write(",");
                writer.Write(@"""__count""");
                writer.Write(" : ");

                writer.Write(string.Format(@"""{0}""", count));
                writer.Write("}");

                writer.Write("})");
                writer.Flush();
            });
        }
        else
        {
            return base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext);
        }
    }

Is there a way to get the a count separately, may be from the underlying provider of the IQueryable ?


回答1:


Try this approach: http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/

It uses the latest Web API OData package.




回答2:


I had the exact issue last week. Check out Extending your ASP.NET Web API responses with useful metadata

I used this post and sample code to get a paging grid up and running using OData. As detailed in the sample I created a delegating handler to capture the HttpResponseMessage and wrap it in custom metadata that includes an item count. A custom attribute, CustomQueryableAttribute, is also created that inherits the default QueryableAttribute.

It may sound a little complex here but is actually pretty simple to implement. I had something up and running in about 30 minutes.

Hopefully future versions of the Web API have more complete OData support.

EDIT: Odata support will NOT be shipping with the Web API. The queryable attribute is being removed for the RTM release. More complete OData support will be available sometime after the initial relase via a separate Nuget package.




回答3:


The SPA DataController (which is derived from the ApiController) used to implement this functionality. However, with the latest changes, it has been removed, as they plan to support OData in a different manner.

If you're working with the MVC 4 Beta, then you can simply change your controller to a DataController, and you're set. If you're using the RC, then you should take a look at an older ASP.NET Webstack commit on Codeplex. The method you're interested in is ExecuteAsync, from DataController.cs



来源:https://stackoverflow.com/questions/11300498/implementing-odata-inlinecount-using-asp-net-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!