Introduction

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

Step 2

Create a Model Class as in the following:

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 { get; set; }
  10. public string Name { get; set; }
  11. public decimal Cost { get; set;}
  12. public string Type { get; set; }
  13. }
  14. }

Step 3

Now add a OData Controller;

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:

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.

Add the following code in the Register method:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Item>("Items");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

Step 5

Now set the port of the application.

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