In today's IT industry Odata is one of the common buzzwords. People are talking about Odata and Odata services. What can Odata do? And in which scenario? We will discuss them a little and then we will see how to expose the Odata endpoints in our Web API application.
So, let's discuss what Odata is. Odata stands for Open Data protocol that was introduced by Microsoft in an open release context then it was widely adapted by the industry. It provides an idea of uniform data access.
We know that today's IT world is on top of data, data may come from various sources and multiple ways and data will be valuable only if we can categorize it easily. Hence the idea of a uniform data access mechanism was created. The industry thought that data might come from multiple locations and multiple protocols but it would be great if we can access data uniformly from all platforms.
For example, an analyst wanted to get data from a Windows data marketplace and SQL Azure using the same mechanism, hence the concept of Odata was created.
Another example of an Odata endpoint is something like that. Let's think of an e-commerce organization sharing their data with a vendor and one vendor wants to get a report based on region by providing a region code where another wants to get a sales report by country by providing a country key. But surprisingly all the data is stored in a single place or in technical terms, in a single table.
The problem is that if tomorrow another vendor demands another type of report by supplying another key then it needs to add another function to serve the needs. This is the exact situation where Odata can rescue us.
Ok, so we have learned the fundamental idea behind Odata, now we will implement an Odata endpoint in our Web API application. In this example we have used Web API 2.0 to implement an Odata service, so please ensure that you have installed the Odata package in your application before executing this code. To install Odata please go to the NuGet Package Manager and install the following package.
Once the package is installed, we can expose an Odata endpoint in the application. In this example we are using Entity Framework and here is the table structure.
Add one .edmx file to the application and generate an entity from the table. Here I have generated the following entity from the table.
Fine, we will now write one simple controller and will expose data as an Odata endpoint and the data will come from the table (that we saw above) using the Entity Framework.
So, let's create the following controller class in the Controller folder of the application.
- 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 OdataAPI;
- using OdataAPI.Models;
-
- namespace OdataAPI.Controllers
- {
- public class personController : ODataController
- {
- private ApiSecurityEntities db = new ApiSecurityEntities();
- [Queryable]
- public IQueryable<UserMaser> Getperson()
- {
- return db.UserMaser;
- }
- }
- }
Please note that the controller class is derived from the Odata controller class, not the WebApiController class and we are returning IQuerable<T> data from the Getperson() action.
This is the key point of an Odata service, rather than returning an object value or HTTP Response message we are returning IQuerable, then the client can query data from this Getperson() endpoint and finally we have decorated the action by a Querable attribute.
Fine, we have done all the setup in the controller section. Now, it's time to register an Odata endpoint in the route. Open the WebApiConfig.cs file of the application and modify it accordingly.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Http;
- using System.Web.Http.OData.Builder;
- using Microsoft.Data.Edm;
-
- namespace OdataAPI
- {
- public static class WebApiConfig
- {
- private static IEdmModel GenerateEdmModel()
- {
- var builder = new ODataConventionModelBuilder();
- builder.EntitySet<UserMaser>("person");
- return builder.GetEdmModel();
- }
- public static void Register(HttpConfiguration config)
- {
-
- config.MapHttpAttributeRoutes();
-
- config.EnableQuerySupport();
-
- config.Routes.MapODataRoute("odata", "odata", GenerateEdmModel());
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- }
- }
- }
Here I have created the GetEdmModel() function where I defined the Entity Class, yes UserMaster is entity name. The Map Odata route takes three arguments, the first one is the friendly name of routing, it could be anything meaningful. The second argument is the root prefix and the third argument is all the Edm Models that we want to expose as an Odata endpoint.
Fine, everything is cool and fine, now let's run the application and try to consume the Odata service from a client. In this example we will use Fiddler as our client.
If we hit
http://localhost:10020/odata/person this URL then we will find the following output.
If we want to get only the first record then we can do a query like this.
http://localhost:10020/odata/person/?$top=1And we will get the first record as output as in the following:
Ok, so we are getting a result from Fiddler. Now what if we need to access this Odata endpoint from a different application? Yes we can access the endpoint from a different application. Let's try to consume the endpoint from a console application. Add a service reference of the Odata endpoint, in my case the URL is:
http://localhost:10020/odata/And let's provide a reference of the URL in the console application.
Then consume the OData endpoint as in the following.
- static void Main(string[] args)
- {
- Uri uri = new Uri("http://localhost:10020/odata/");
- var container = new OdataPerson.Container(uri);
-
- var data = container.person.Where(fn => fn.id == 1).FirstOrDefault();
- Console.WriteLine(data.name);
- Console.ReadLine();
- }
ConclusionIn this article we saw how to expose and consume an Odata endpoint in a Web API 2 application. I hope you have understood it and liked. Thanks, happy learning.