Deploying oData inlinecount using asp.net webapi

Possible duplicate:
Single pagination using WebApi ($ inlinecount)

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

The problem is that in order to get the results in the count element, they need results in order to make swap work on the server side, so now I hacked the formatter to get a fake counter for work. All this works fine, and the grid is all happy, however, getting a real account is a problem, since the returned IQueryable expression already has filters / Take for it, etc.

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 counter separately, maybe from the main IQueryable provider?

+1
source share
3 answers
+1
source

I had the exact number last week. Check out the ASP.NET Web API response extension with useful metadata

I used this post and sample code to get a paging grid and run using OData. As indicated in the sample, I created a delegation handler to capture the HttpResponseMessage and wrapped it in user metadata that includes item counting. A CustomQueryableAttribute attribute is also created, which inherits the default QueryableAttribute attribute.

This may seem a bit complicated, but actually quite simple to implement. I earned about 30 minutes.

We hope that future versions of the web API have more complete support for OData.

EDIT: Odata support will NOT ship with the web API. The queryable attribute is removed for RTM release. Better OData support will be available some time after the initial upgrade through a separate Nuget package.

+2
source

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

If you are working with a beta version of MVC 4, you can simply change your controller to a DataController and you are set up. If you are using RC, you should take a look at the old ASP.NET Webstack commit on Codeplex. This method that interests you is ExecuteAsync , from DataController.cs

0
source

All Articles