OData Programming Cookbook for .NET Developers
上QQ阅读APP看书,第一时间看更新

Exploring an OData service through web browser

What is the simplest and most convenient means to access data entities exposed from an OData service or shall we always build a dedicated client proxy or use some OData client APIs to consume OData service? Of course not! Actually, what we need is just a web browser. By using a web browser, we can explore the metadata of an OData service and query any entity set exposed in the service in a quick and straightforward way. In this recipe, we will show you how to use a web browser to quickly explore the data exposed from an OData service.

Getting ready

The sample OData service we will use here is still based on WCF Data Service and ADO.NET Entity Framework. The Northwind database will be used as the backend data source.

The source code for this recipe can be found in the \ch02\ NWDataServiceSln\ directory.

How to do it...

  1. Create a new ASP.NET Empty Web Application.
  2. Create a new WCF Data Service with the ADO.NET Entity Framework data model (using the Northwind database).

    The sample service needs to expose the following entity sets (from the corresponding Northwind database tables):

    • Categories
    • Products
    • Orders
  3. Select the .svc service file in the web application and launch it using the View in Browser context menu (see the following screenshot).
    How to do it...
  4. View the metadata of the sample service by using the $metadata URL suffix.

    We can directly append the $metadata suffix after the base URL of the sample service. The following screenshot shows the entity type definitions contained in the service metadata:

    How to do it...

    We can expand and collapse the elements in the metadata document to view data types and relations defined in the service.

    Tip

    The $metadata URL suffix is a WCF Data Service specific convention. Other OData service implementations might use different URL conventions for exposing service metadata.

    Under the entity type definitions, we can also find the definitions of entity sets exposed in the service (see the following screenshot).

    How to do it...
  5. View all entities within a given entity set by appending the entity set name after the base URL.

    The following screenshot shows the web browser output by accessing the Categories entity set:

    How to do it...
  6. Access a specific entity instance by supplying the entity key value in the URL.

    For example, we can use the following URL address to access the Category entity object whose key is 2 (see the following screenshot).

    How to do it...
  7. Navigate between entities or entity sets through reference or navigation properties.

    When exploring the OData service metadata, we can find that there are some Association and AssociationSet elements defined together with the entity and entity sets. By using such association information, we can navigate from one entity object to its associated entity or entity collection. We can use the following URL to access the Product entities associated with the given Category entity:

    http://[server]:[port]/NWDataService.svc/Categories(2)/Products

    The following screenshot shows the web browser output by executing the previous URL query:

    How to do it...
  8. Apply some OData query options in the URL.

    The following are two sample URLs, which have used the $filter and $top query options:

    • retrieve all order entities that have 'Brazil' as ShipCountry

      http://[server]:[port]/NWDataService.svc/Orders?$filter=ShipCountry eq 'Brazil'

    • retrieve the top 3 Order entities from service

      http://[server]:[port]/NWDataService.svc/Orders?$top=3

    The following is the web browser output by executing the second query given previously:

    How to do it...

How it works...

Now, we know that we can use a web browser to view the entity sets exposed in OData services (with the corresponding URL addresses). It is because OData is based on HTTP protocol and enables data access via standard HTTP methods (such as GET, POST, and PUT). This also makes it quite convenient for any HTTP web-request-enabled client to access the data exposed by OData services.

Metadata is also very important to OData clients. By using the metadata document, we can get all the entity set and entity types' information of an OData service. And by using standard HTTP URL format addresses, we can access any kind of the following data resources exposed in OData services:

  • An entity set
  • An entity object
  • Associated entity collection on a specific entity object
  • Custom service operations
  • An entity collection based on query filters

Even if you haven't got an overview of the entire service data structure (by using metadata), it is still quite straight and intuitive for you to discover the data exposed in an OData service. We can do it by accessing the default service document at the base service address. For example, we can view the default document of the sample Northwind OData service by typing the following URL in the web browser:

http://[server]:[port]/NWDataService.svc/

The default service document will show you all the exposed entity sets and their relative locations (through the href attribute). By appending the href relative address after the base service URL, we can drill into the specific entity set (such as the following Categories entity set URL).

http://[server]:[port]/NWDataService.svc/Categories

And if you add an entity key value in the entity set URL, you can view the data of a specific entity object only (see the following sample URL of a specific Category entity object).

http://[server]:[port]/NWDataService.svc/Categories(2)

Since each Category entity is associated with a collection of Product entities, you can further extend the query URL (by appending the Navigation property name) so as to view all the associated entity objects. The following URL is used to get all Product entities associated with a specific Category entity:

http://[server]:[port]/NWDataService.svc/Categories(2)/Products

By using this intuitive and self-explained data presentation style, it is quite handy for us to explore an OData service in a standard web browser and you will find it quite useful when you develop and test an OData service.

We have also shown some sample URLs, which use query options. We will discuss query options further within a dedicated recipe in this chapter.

There's more...

In case you want to know more about the OData metadata format and how we can query it, the following article is worth reading:

Queryable OData Metadata available at http://www.odata.org/blog/2010/4/22/queryable-odata-metadata

See also

  • Filtering OData query results by using query options recipe