Quick Code
Published in

Quick Code

Creating Open Data In .Net Development

A Guide To Create An OData Service In .Net Web Development

Creating OData Services makes data access easy in application development, let’s know everything about OData in .net development.

Image source

.Net is one of the world’s most popular technologies. And! Every year Microsoft releases a number of updates and new features that make application development easier with .Net. One such feature is oData.

Surely, you would have heard a lot about OData these days. The major reason for its popularity in the .Net web development space is the ease in performing CURD operations. Let’s know what OData is?

What Is oData?

Per the experts who crafted oData, it is the best way to REST.

As you know that REST is an architectural style that offers a set of constraints and sub-constraints. You are already aware that client-server interaction should be stateless, and each message should be abundant information that should describe the way to process the message, and so on.

But REST isn’t standard at all. However, it leaves some doors open for developers and to decide. There are always some guidelines for resources, and you would have seen resources like “api/product/1” or “api/products/1”. These are examples that can be referred to exactly the same.Asp.Net developers call it biggest ease in development,

(OData Architecture)

But while they are compliant with REST, many of these are not developer-friendly. It is crucial when the project you’re working on consists of all these different resource naming styles. HATEOAS is one more exotic constraint that comes in the way of implementing a RESTful system. It hinders when it comes to defining the contracts for those leaves more open for interpretation.

Now comes OData in the picture, as it works as a mechanism to standardize REST. OData is proven to be an open protocol that makes it simple for you to create & consumption of queryable and interoperable RESTful APIs in a standard way. It works as a bridge that describes things that use the HTTP method for the type of request.

Besides, the information on querying data for filtering and paging are major inclusions. Not just this, calling custom functions & actions, working with batch requests are also a major part of this process.

If you want to leverage the ease of OData in your .net application development, you can connect with a certified .net developer.

Now you would be wondering how you can implement OData within your API. Let’s look at the step-by-step process.

Prerequisites — Integrating The Correct NuGet Packages

Adding the OData Nuget Package is the first thing you’ll have to do once you start a new API project. For which you would want a Microsoft.AspNetCore.OData. It supports .NET Core and .NET 5. Version 7.x.

With OData Nuget, we also need the Core NewtonsoftJson to input and output format parameters. Right-click on the “project” in solution-explorer and click on the Manage NuGet; that you can find these packages in the browser section.

image source

Integrate Model Class / Defining and Entity Data Model

What is a data model? It is actually an abstract data model that describes the data used (exposed ) by an OData Server. It can also be termed as the central unit of your OData services. The major purpose of introducing this concept is to describe the structure of data despite whatever is its stored form.

EDM( Entity Data Model) Makes stored data form of no use and of less significance to application design and development. Moreover, the entities and relationships describe the structure of data for its use in the application. Moreover, the evolution of this data format surges with respect to the application evolution.

Hers is the example shown of ED exposes a set of people and product records.

Let’s see how to create a model class.

From here, you need to right-click on the solution project in solution explorer, select Add from the pop-up menu, select NewFolder, name the New Folder as Models.

Now it’s time to add the class to the Models. Here we use the POCOs class (Plain Old CLR Object) to represent the Model Products.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace OData_API.Models


public class Products


public int productId { get; set; }

public string productName { get; set; }

public string productCategory { get; set; }

public string prodcutPrice { get; set; }

public int productDesc { get; set; }



Making an OData Service

You would have to derive data from ODataController; defining OData service is essential. ODataController is the base class that supports the reading and writing of data via OData formats. The ControllerBase is the source where the whole data processes, and it supports most of the actions (related controller) you can expect to be used while creating an API or in the .net web app development.

However, It is based on OData routing principles despite working on a default routing basis. Similar is a fact for serialization as it avoids the default ASP.Net core format and works with OData-specific formatters. Moreover, two additional methods are integrated to create action results to respond to POST and action results to respond to manipulators.

Let’s look at how to create an OData service for Product records.

public class ProductController : ODataController


private readonly CompanyRecordsDbContext _companyRecordsDbContext;

public ProductController(ConpanyRecordsDbContext companyRecordsDbContext)


_companyRecordsDbContext = companyRecordsDbContext

?? throw new ArgumentNullException(nameof(companyRecordsDbContext));




public IActionResult Get()


return Ok(_companyRecordsDbContext.Product);




public IActionResult Get(int key)


var people = _companyRecordsDbContext.Product.Where(p => p.ProductId == key);

if (!product.Any())


return NotFound();


return Ok(SingleResult.Create(product));



The above code is written for performing two major actions- one is to get a specific product name, another is to get all products. You can see that it is already mentioned that these are to be routed to when the GET function is leveraged by HttpGet attribute.

However, there is no accrued necessity but conventional. Actually, the “Get” function will automatically be mapped to the Http get method. And the “Product” part of the controller will be considered as part of the route.

It means ODataRoute isn’t 100% necessary. Moreover, Microsoft also provides a notation to refrain from convention-based routing for APIs and mostly depends on constraints despite using attribute-based routing.

Besides, if you hire full-stack .net app developers, you can leverage the potential of OData abundantly.

To get all products, a GET request can be sent to scheme://host:port/odata/Products.

GET scheme://host:port/odata/Products

Querying Data Using Json

Accept: application/json

Take a look here at the way OData returns the product. To get the data of a specific product, a GET request should be sent to


Here “1” is the Id of the product. You can also check out the URL syntax guidelines here. The identifiers are given inside the brackets and are not available in the URL fragment.

GET scheme://host:port/odata/Product(1)

Accept: application/json


“@odata.context”: “https://localhost:44376/odata/$metadata#Product/$entity",

“PersonId”: 1,

“Name”: “Sport Shoe PoloX”,

“Size”: “28”,

“Price”: “$50”,

“NumberOfOrders”: 30,

“TotalProductAvaialble”: 60


This returns one specific product detail. Isn’t it an easy process to get data from OData?

Well! By now, you would have understood the significance of OData and how you can use it to get Data from the source. Creating OData APIs is simple and doesn’t bother .net developers when it comes to implementation and streaming a huge set of data within the application.

Final Text

OData is incredibly useful in .Net development; that’s the reason every Asp.net development company in India ensures to leverage it in their development. Creating OData RESTful APIs can improve your application performance and can bring in more ease in development. For more insights about modern tech, stay tuned with me here.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amyra Sheldon

I am a tech enthusiast, project manager and a passionate writer with digital thinking. I write about latest technologies ie Blockchain, IoT, AI for ValueCoders.