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