Click here to Skip to main content
13,352,175 members (73,678 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

18K views
10 bookmarked
Posted 16 Apr 2014

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

, 16 Apr 2014
Rate this:
Please Sign up or sign in to vote.
How to build 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 jumping into the code samples, let’s talk a little bit about OData definition and specifications.

OData Introduction

OData stands for Open Data Protocol. It is standard 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 is that it builds on top of mature standard web technologies and protocols such as HTTP, Atom Publishing Protocol, JSON, and follows 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 that will 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 we start building our OData service, let’s examine querying an existing OData service published on the internet and use 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 that you can use Linq4Pad to generate and test complex OData queries.

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

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

<!-- #tablepress-1 from cache -->

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 Version Version 1, 2, and 3 Version 1, 2 and 3 Version 3
Metadata and Hyper Links Data and MetaData Data and Metadata No Metadata, Just the Data
Payload Size for all Products entity 28.67 KBs 14.34 KBs, smaller by 50% 4.25 KBs, smaller by 75%
Easy to consume in mobile clients No Yes Yes
Accept Header application/atom+xml application/json;odata=verbose application/json

<!-- #tablepress-2 from cache -->

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

The 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.

The post Building OData Service using ASP.Net Web API Tutorial – Part 1 appeared first on Bit of Technology.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Taiseer Joudeh
Architect Aramex
Jordan Jordan
Taiseer Joudeh has more than 8 years of experience spent in developing and managing different software solutions for finance, transportation, logistics, and e-commerce sectors. He has been deeply involved in .NET development since early framework versions and currently he works on different technologies on the ASP.NET stack with deep passion for Web API, and Microsoft Azure.

Recently Taiseer has been focusing on building Single Page Applications and Hybrid Mobile Solutions using AngularJS.

Taiseer lives in Jordan with his wife and son, works as IT Manager at Aramex, also he is a regular speaker in local events and Dev user groups, he is a avid blogger on http://bitoftech.net, and you can follow him on twitter @tjoudeh

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 16 Apr 2014
Article Copyright 2014 by Taiseer Joudeh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid