Since VS 2012.2 update ASP.NET Web API can take advantage of the [ODATA Query parameters. Having the API consumer dictate part of the query can be powerful yet something that naturally needs some performance and security consideration before implementation.
Enabling the support is as simple as decorating the controller class or selected methods with the ActionFilterAttribute Queryable.
Now the API automatically supports things like $top, $orderby, $filter directly on the URL. 🙂
So a HTTP GET to http://server/service/api/wines?$top=2&filter=WineType eq ‘Red’ would get me this response
You can control what queries to allow using [properties on the ActionFilter Queryable.
One thing missing in the Web API ODATA implementation compared to the full-blown ODATA implementation in WCF Data Services is the $select keyword. The $select lets the API consumer pick what fields to return. With large objects this can save both bandwidth and serialization/deserialization performance.
The fact that ASP.NET Web API is open source in combination with the nightly-builds available on MyGet makes testing upcoming features super easy. Since a week or two back both $select and the $expand are available in the nightly-builds. (http://www.myget.org/gallery/aspnetwebstacknightly).
To install run this command:
install-package Microsoft.AspNet.WebApi.OData -pre -source http://www.myget.org/F/aspnetwebstacknightly
After installation i can now make a HTTP GET like this
http://server/service/api/wines?&$top=2&$filter=WineType eq ‘Red’&$select=Id,Name and get only the ‘Id’ and ‘Name’ fields in the response.
The nightly-build i tried (5.0.0-beta1-130511) only had $select support when using JSON. With XML it causes a serialization exception. I guess supporting this with the DataContractSerializer would require alot more work compared to what they had to do with JSON.
Anyway – i think its a nice feature so thanks to the team working at getting this feature into the framework in a future release and the opportunity for me to beta-test it already today 🙂