This post is about getting started with OData in ASP.NET Core. OData (Open Data Protocol) is an ISO/IEC approved, OASIS standard that defines a set of best practices for building and consuming RESTful APIs. OData helps you focus on your business logic while building RESTful APIs without having to worry about the various approaches to define request and response headers, status codes, HTTP methods, URL conventions, media types, payload formats, query options, etc. OData also provides guidance for tracking changes, defining functions/actions for reusable procedures, and sending asynchronous/batch requests.
ASP.NET Core didn’t support OData officially. This blog post using a NuGet package, which looks like official, but it is not.
First you need to create a Web API project with dotnet command. Once it is created, you need to add the OData package to your project, you can use the following command to do it.
Next you need to create a model class and DBContext class. I am using an InMemory database. Here is the model class and DB Context class.
Next you need to create a Controller, in earlier versions of OData you can inherit from ODataController. But in ASP.NET Core, there is no OData controller available. So you need to create a normal controller, with OData attributes.
For EnableQuery attribute you require “Microsoft.AspNetCore.OData” namespace. Finally, you need to modify your startup class code to add OData middleware and OData routing.
Without the routing prefix - OData in this example, all the routes will be redirected to OData endpoints.
And here is the output of $metadata endpoint.
While developing the application, I faced three issues.
- InvalidOperationException: Cannot resolve scoped service ‘Microsoft.OData.ODataSimplifiedOptions’ from root provider.
I got this exception while working with application. I had to spent some time exploring this issue. And finally resolved it by adding following code in
Main() method in program.cs
- Some serialization exception while returning
SingleResultfrom controller action -
Unfortunately, I was not able to fix this exception. As a workaround, I did something like this.
- HTTP PUT was not working - Due to some strange issues, HTTP PUT was not working. It is always returning 404. I was not able to resolve this issue and I don’t have any workarounds for this.
Source code available on GitHub
Happy Programming :)