TechBubbles Microsoft Technology BLOG


What is Web API? It is a Framework for building HTTP services on top of the .NET Framework. To get familiarise yourself with ASP.NET Web API then you can read this post here. 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. More on this topic can be read here. What is ASP.NET Web API OData? It is a framework that you can use OData services in your Web API application. This post describes the details of using ASP.NET Web API with OData and also outlines the features of ASP.NET Web API OData and steps to create Web API OData service using Visual Studio 2012.

ASP.NET Web API OData features

What are the different components available for building OData Services?

Model Builders( Example: EDM model builder), You can use Formatters like OData formatter, Atom , JSON and XML to expose your service to external world. Path and Query parsers can be used to parse OData URI’s. You can use LINQ expression generator for exposing IQueryables through OData service.

Why OData service? OData comes with the common patterns over HTTP like querying, paging, metadata and relationship. You would not get these features if you develop a plain HTTP service. OData has a rich client echo system, Excel , JavaScript libraries like datajs can consume this service.

Writing a OData Service

1. Create a new console application project in Visual Studio 2012 and create a database connection in server explorer. We are using self-host which comes with Web API.


2. Install Entity Framework  NuGet package which you can use to build the data model.


3.Right click on the project and select Entity Framework option , If do not see Entity Framework option in Solution Explorer then install Entity Framework power tools from here. Use Entity Framework Reverse Engineer Code First option to create this model and generate mapping classes


The generated models looks as below


4. Now install Microsoft Web API self host package using package manager console as shown below, You can also use Manage Nuget packages screen to install the same. This package is to host the Web API service


5. Install the OData package using the below console command. It is a pre-release version.


6. Create an EDM model with below code. You can use ODataconvention model builder class to build your model, also tell the model what entity sets you have


7. Once your EDM model is ready then build the OData Service by defining the routes and start the service using below code


8. The service is ready now and execute the program and examine the service document which OData service exposes using fiddler tool. This service document is located at the root of the service and it tells you the metadata information about the service


9.You can use $metadata in the URL to see what entity types and entity sets that your service is exposing


10. Now if you try to send a request to the entity set in your model then you will get an 404 error. You need to write a controller to expose the data from the entity set as shown below


About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

1 Comment

  • Have you a solution when the data object is not fix? At Product, some fields are always the same an some fields are dynamic by customer.
    So, the ODATA result has different fields.

By Kalyan Bandarupalli
TechBubbles Microsoft Technology BLOG

Follow me


Tag Cloud