Techbubbles

OData Querying Options in ASP.NET Web API

What is OData? Open Data Protocol is a web protocol for accessing information via services. It is built upon technologies such as XML and JSON. OData allows you to exposing the data and metadata in a standard way. OData Querying is a query language which allows the clients to send the OData defined parameters in the query string of the request URI. This post discusses the various OData query options that you can use using ASP.NET Web API.

The below class exposing the Entity Framework entity set movies.

image

To enable the OData query options on Entity set add Queryable attribute to Get method

image

To get the movies that released in year 2012 from the database, you can write the below query

image

Using an and operator in querying options, for example to get the movies that released in year 2012 and got the rating greater than or equal to 9

image

If you want to list the movies order by popularity then you can use orderby query option. You can use desc to list the movies in descending order.

image

You can use Top and skip query options as shown below

image

You can add paging support by adding an additional parameter PageSize

image

It then automatically adds the arrows to your page

image

OData Web API supports three formats JSON, XML and ATOM feed. The more samples on this topic can be found here

Reference: Youssef talk from Channel9

Related Posts:

%d bloggers like this: