Part 05 – OData v4 in ASP.NET WebApi – Querying 1

This is a series of blogs detailing my introduction to using OData v4 with C# and ASP.NET WebApi.

Query OData Entities

Once we have the OData model defined we can start to query the entities.

Within the EntityContainer in the metadata document there are four EntitySets defined.

Let’s begin running some queries against them. The URL follows the format http:////.

Note: The demo Trip Pin service allows uses to perform writes as well as reads and you will see references in the metadata to urls with additional data. E.g. http://services.odata.org/TripPinRESTierService/(S(1iit5imcudlfb3hd1duwtznk))/. You can ignore the value after the service for the querying demonstrated here.

Query AirLine Entity Set

From Postman let’s look at the Airlines entity set. Issue a GET request to http://services.odata.org/TripPinRESTierService/Airlines

The entity set returns three Airline entities with properties AirLineCode and Name. We know from the metadata document that AirLineCode is a key and therefore the values are unique.

OData Attributes and Controlling What is Returned

You will notice that the JSON response includes @odata attributes. We have already seen @odata.context. There is also a @odata.etag included. It does not matter what they all mean at the moment however; when any are needed I will mention them. You can control what attributes are returned based on the needs of the client application.

By adding an Accept header in the request, we can control the amount of detail returned. By default the request will return minimal. We have two other options.

application/json; odata.metadata=none

This excludes all odata attributes.

application/json; odata.metadata=full

Now all the odata attributes generated by the server are returned.

In the next blog we will continue looking at querying OData.