Work With Odata in Web API: Create Your First 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 up gradation, 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.

public static void Register(HttpConfiguration config)

{

    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

    builder.EntitySet<Models.personModel>("Person");

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

     config.EnableSystemDiagnosticsTracing();

}

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.

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.Entity;

using System.Data.Entity.Infrastructure;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using System.Web.Http.ModelBinding;

using System.Web.Http.OData;

using System.Web.Http.OData.Routing;

using MvcApplication2.Models;

 

namespace MvcApplication2.Controllers

{

    public class personController : ODataController

    {

        [Queryable]

        public IQueryable<personModel> Getperson()

        {

                List<personModel> list = new List<personModel>{

                new personModel{Id=1, name = "Sourav",surname="kayal",age=26},

                new personModel{Id=2, name = "Ram",surname="Das",age=26}

            }; 

            return list.AsQueryable<personModel>();

        }

    }

}

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.