Work With Odata in Web API: Create Your First Odata Service

This is the “Work with Odata in Web API” article series. This article of the series explains various parts of Odata service in context of the ASP.NET Web API. The previous article provided an introduction to Odata. You can read it here:

Work With Odata in Web API : Introduction of ASP.NET Odata service

In this article, we will create our first Odata service in the ASP.NET Web API 2 environment. It's a highly practical example of a simple Odata service. I will assume you have a basic understanding of the MVC architecture and Web API service.

Ok, if you still want to continue then that implies you have the required skills and want to learn Odata services in the Microsoft platform. Fine, use the following procedure and you will get it done.

Create one MVC 4 project in Visual Studio

Don't forget to select the MVC 4 template otherwise we will not get a template of the Web API. If you are using Visual Studio 2010 then you need to install the template externally.

Create one MVC project

Select Web API project

Make sure you have selected a Web API project and the view engine part is not necessary and not important in our example, so leave it as it is.

Web API

Once you click on OK, the project will be created in the Visual Studio environment and the next step is to install a few packages from the Nugget Package Manager.

Install Web API 2 package

In my case, I opened the Web API version 1.0 application. To support Odata, we need to upgrade the Web API version. So, go to the Nuget Package Manager and search for the Web API 2.0 package.

Install Web API 2 package

Once you press install you will see the package being installed and in the middle of the installation you might encounter the following screen. It's asking you to accept the terms and condition of dependent packages. Just click “Accept” and proceed.

terms and condition

Once we finish our Web API upgradation, we will install the Odata service package from the same package manager. Search for “odata service package” and install the following package in the application.

odata service package

Again in the middle of the application you will encounter the following screen that will ask to accept the license agreement. Click “Accept” and proceed.

accept license agreement

Fine, we have set up our packages and all installation has finished. Now, we can start to write code to expose our Odata service.

Create Model class

At first we will create a Model class that we will expose as a data model in the Odata service. In this example, we created a “person” model class as in the following. It has four properties.

Model class

Add Odata controller

If you are experienced with MVC then you're familiar with the MVC controller but in an Odata service the controller is the Odata controller that we will add to the application shortly.

So, right-click on the controller folder and click on the menu as in the following screen.

Add Odata controller

controller

It will open the following window. At first we need to provide a controller name and then model name. In the data context box just click on the "Add data context" button and provide the name as in the following screen.

controller name

Configure Odata route

This is a very important part of Odata service configuration. Open the “WebApiConfgi.cs” file from the App_Start folder. We need to register the route of our Odata servce.
  1. public static void Register(HttpConfiguration config)  
  2. {  
  3.     ODataConventionModelBuilder builder = new ODataConventionModelBuilder();  
  4.     builder.EntitySet<Models.personModel>("Person");  
  5.     config.Routes.MapODataRoute("odata""odata", builder.GetEdmModel());  
  6.      config.EnableSystemDiagnosticsTracing();  
  7. }

Implement Get() method in controller

Here we have implemented a simple controller that will expose the service. In the Get() method we have just hard-coded some data. In reality obviously you will use a DB to fetch the data.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Data.Entity.Infrastructure;  
  6. using System.Linq;  
  7. using System.Net;  
  8. using System.Net.Http;  
  9. using System.Web.Http;  
  10. using System.Web.Http.ModelBinding;  
  11. using System.Web.Http.OData;  
  12. using System.Web.Http.OData.Routing;  
  13. using MvcApplication2.Models;  
  14. namespace MvcApplication2.Controllers  
  15. {  
  16.     public class personController : ODataController  
  17.     {  
  18.         [Queryable]  
  19.         public IQueryable<personModel> Getperson()  
  20.         {  
  21.             List<personModel> list = new List<personModel>{  
  22.             new personModel{Id=1, name = "Sourav",surname="kayal",age=26},  
  23.             new personModel{Id=2, name = "Ram",surname="Das",age=26}  
  24.             };   
  25.             return list.AsQueryable<personModel>();  
  26.         }  
  27.     }  
  28. }

Fine, our Odata service is now ready to be exposed. Let's run the application and try to consume the Odata service. Open a browser window and point to the Get() action in the person controller.

Odata service
Oh, Cheers; our first Odata service is running successfully. We are getting a list of all persons.

Now, if we want to filter the results with a query, something like this:

filter the result
Have a look at the URL part. In the URL we are passing a query to get the information with the Id 1. Anyway, we will explain the query in a later article.

Conclusion

This article implemented an Odata service practically. I hope you have understood it and successfully implemented it. In a future article, we will understand more about Odata services.


Similar Articles