OData Endpoints in Web API


This article explains OData  Endpoints in the Web API. OData is a web protocol for accessing the data. It uses web technologies such as Atom Publishing, HTTP and JSON. Atom Publishing is also called AtomPub. We can easily create an OData Endpoint by the Web API OData. We can also controlled the OData operations that are supported by the OData Endpoints.

Use the following procedure to create a sample application.

Step 1

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • Select "Installed" -> "Templates" -> "Visual C#" -> "Web" and select ASP.NET Web Application.

    Select Web Application

  • From the ASP.NET project window select "Empty" and select the "Web API" check box.

    Select API Project

  • Click on the "OK" button.

Step 2

Create a Model Class as in the following:

  • In the "Solution Explorer".
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".

    Create Model Class

  • Select "Installed" -> "Visual C#" and select class.

Add the following code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace ItemAPI.Models  
  6. {  
  7.     public class Item  
  8.     {  
  9.         public int ID { getset; }  
  10.         public string Name { getset; }  
  11.         public decimal Cost { getset;}  
  12.         public string Type { getset; }  
  13.     }  
  14. }  

Step 3

Now add a OData Controller;

  • In the "Solution Explorer".
  • Right-click on the Controller folder.
  • Select "Controller" -> "Web API2 OData Controller with actions using Entity Framework".

    Select OData COntroller

  • In the Add Controller Dialog box, name the Controller as "ItemsController" and select the "Use async controller actions" , now form the model dropdown list select model class.

    Set Name and Model of controller

  • Click on the "New Data Context".

    Select Context

  • Click on the "Add" button.

    Create OData Controller with Entity

  • Now again click on the "Add" button.

If there is an error message generated then before creating the OData controller build the project and then create the controller.

Create two code files as in the following:

  • The First is "ItemsController" that defines the Web API Controller.
  • The Second is "ItemAPIContext.cs" that defines the methods to query data with Entity Framework.

    Display Controller and Context file

Step 4

Now add the OData Entity Data Model. When OData creates a system that defines all the entities in the metadata document, this is called EDM. We use the "ODataConventionModelBuilder" for creating the EDM.

  • In the "Solution Exploere".
  • Select the "App_Start" folder and select  the "WebapiConfig.cs" file.

 Add the following code in the Register method:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

Step 5

Now set the port of the application.

  • Right-click on the project.
  • Select properties and then select "Web".
  • Now set the URL in the "Project URL".

    Set URL

  • Click on "Create Virtual Directory".

Step 6

Execute the application by pressing F5.

Open the fiddler, click on the composer tab and type this URL: "" then click on the "execute" button.

Type the URL in fiddler

Now Fiddler send the HTTP request to the application. This response is in the left panel of Fiddler.

See Response

Double-click on the response to see the details in the "Inspectors" tab.

Detail of Response