Building OData Service using ASP.Net Web API Tutorial – Part 1

In my previous tutorial we’ve covered different aspects of how to Build RESTful service using Asp.NET Web API, in this multi-part series tutorial we’ll be building OData service following the same REST architecture we’ve talked about previously.

Before jump into code samples let’s talk a little bit about OData definition and specifications.

OData Introduction

OData stands for Open Data Protocol. It is standard to for providing access to data over the internet, OData is championed by Microsoft as an open specification and adopted by many different platforms. By using OData it will allow you to build a uniform way to expose full featured data APIs (Querying, Updating), the nice thing about OData that it builds on top of mature standard web technologies and protocols such as HTTP, Atom Publishing Protocol, JSON, and follow the REST architecture in order to provide data access from different applications, services and data stores.

It is well known that any service follows the REST principles well adhere to the aspects below:

  • Resources are identified by a unique URI only.
  • Actions on the resource should be done using using HTTP verbs (GET, POST, PUT, and DELETE).
  • Enable content negotiation to allow clients specifying the format of the data type returned: XML format (AtomPub), or JSON format (Verbose/Light).

odata service asp net web api

Querying Existing OData Service

Before start building our OData service let’s examine querying an existing OData service published on the internet and uses the Northwind database, the base URI for this service is http://services.odata.org/Northwind/Northwind.svc. You can use any REST client such as (Fiddler, PostMan) to compose those HTTP requests, as we are only querying  the service now (No sending updates) you can use your favorite browser as well. Note you can use Linq4Pad to generate and test complex OData qyeries

The tables below illustrates some of the OData query options which can be used to query the service:

Option
OData Service URL
Notes
$filterhttp://services.odata.org/Northwind/Northwind.svc/Products?$filter=ProductName eq 'Tofu'Filter the results based on Boolean condition, I.e. get product name = 'Tofu'
$orderbyhttp://services.odata.org/Northwind/Northwind.svc/Products?$orderby=ProductNameSort the results, i.e: Sort the products by Product Name
$skiphttp://services.odata.org/Northwind/Northwind.svc/Products?$skip=10Skip the first n results, used for server side paging
$tophttp://services.odata.org/Northwind/Northwind.svc/Products?$top=10Returns only the first n the results, used for server side paging
$selecthttp://services.odata.org/Northwind/Northwind.svc/Products?$filter=ProductName eq 'Tofu'&$select=ProductName,UnitPriceSelect which properties to be returned in the response. i.e. returning ProductName and UnitPrice
$expandhttp://services.odata.org/Northwind/Northwind.svc/Products?$expand=SupplierExpand the related entities inline i.e. expand the supplier entity for each product
$inlinecounthttp://services.odata.org/Northwind/Northwind.svc/Products?$inlinecount=allpagesInform the server to return the total count of matching records in the response.

As we see in the table above we can combine different query options together and provide complex search criteria for example if we want to implement server side paging we can issue the following HTTP GET request: http://services.odata.org/Northwind/Northwind.svc/Products?$top=10&$skip=0&$orderby=ProductName&$inlinecount=allpages where $skip represents the number of records to skip usually (PageSize x PageIndex) and $inlinecount represents the total number of products. To view the complete list of available query options you can visit this link.

As we stated before OData service allows content negotiation, so the client can choose the response format by setting the Accept header of the request, each response format has its own pros and cons, the table below illustrates the differences:

 
XML (Atom Publishing)
JSON Verbose
JSON Light
OData VersionVersion 1, 2, and 3Version 1, 2 and 3Version 3
Metadata and Hyper LinksData and MetaDataData and MetadataNo Metadata, Just the Data
Payload Size for all Products entity28.67 KBs14.34 KBs, smaller by 50%4.25 KBs, smaller by 75%
Easy to consume in mobile clientsNoYesYes
Accept Headerapplication/atom+xmlapplication/json;odata=verboseapplication/json

I’ve decided to split this tutorial into four parts as the below:

Source Code for this series is available on GitHub, you can download it locally or you can fork it. If you have any question or if there is nothing unclear please drop me a comment.

Building OData Service using ASP.Net Web API Tutorial – Part 3

web api odata crud

This is the third part of Building OData Service using Asp.Net Web API. The topics we’ll cover are: OData Introduction and Querying Existing OData Service - Part 1. Create read-only OData endpoint using Asp.Net Web API - Part 2. CRUD … [Continue reading]

Building OData Service using ASP.Net Web API Tutorial – Part 2

odata service asp net web api

This is the second part of Building OData Service using Asp.Net Web API. The topics we’ll cover are: OData Introduction and Querying Existing OData Service - Part 1. Create read-only OData endpoint using Asp.Net Web API - Part 2 (This … [Continue reading]

What is New in ASP.Net Web API 2 – Part 2

In the previous post we've covered ASP.Net Web API 2 attribute routing, in this post we'll complete covering new features, we'll start by discussing the new response return type IHttpActionResult then cover the support for CORS. Source code is … [Continue reading]

What is New in ASP.Net Web API 2 – Part 1

Web API 2 Project Template

Asp.Net Web API 2 has been released with the release of Asp.Net MVC 5 since 5 months ago, Web API 2 can be used with .NET framework 4.5 only, the Web API 2 template is available by default on VS 2013 and you can install Web Tools 2013.1 for VS 2012 … [Continue reading]

Building ASP.Net Web API RESTful Service – Part 11

WebApiCachingETag

This is the eleventh part of Building ASP.Net Web API RESTful Service Series. The topics we'll cover are: Building the Database Model using Entity Framework Code First - Part 1. Applying the Repository Pattern for the Data Access Layer - Part … [Continue reading]