OData (Open Data Protocol) in ASP.NET 6.0

September 13, 2021 by Anuraj

OData AspNetCore

OData (Open Data Protocol) is an ISO/IEC approved, OASIS standard that defines a set of best practices for building and consuming RESTful APIs. This post is about implementing OData (Open Data Protocol) in ASP.NET 6.0. OData RESTful APIs are easy to consume. The OData metadata, a machine-readable description of the data model of the APIs, enables the creation of powerful generic client proxies and tools. Unfortunetly OData is not available as part of Minimal APIs. So first let’s create a Web API application using the dotnet new webapi command. Once you created the API project add reference of Microsoft.AspNetCore.OData package.

Next you can modify the builder.Services.AddControllers() method in Program.cs like this.

using Microsoft.AspNetCore.OData;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers()
    .AddOData(options => options
        .Select()
        .Filter()
        .Expand()
        .SetMaxTop(100)
        .Count()
        .OrderBy());
        
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "WeatherForecastApi", Version = "v1" });
});

Now you’re configured ASP.NET Core app to work with OData. And finally you need to add the EnableQuery attribute to the controller Get method.

[HttpGet]
[EnableQuery]
public IEnumerable<WeatherForecast> Get()
{
    return Enumerable.Range(1, 25).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

Now you’re ready to consume the endpoint with OData protocols.

You can open your browser and open the following URL - https://localhost:5001/WeatherForecast?$select=Summary and you will be able to see like this.

OData select query

The select query you help you to select a particular field from the JSON response. You can filter the results using a query like this - https://localhost:5001/WeatherForecast?$filter=Summary eq 'Hot' - which will return JSON results where Summary field is Hot. You can order by fields using Order by parameter - https://localhost:5001/WeatherForecast?$orderby=Date - this will return results ordered by the Date. Since the MaxTop method is added, you can get paged results as well - Like the LINQ paging - https://localhost:5001/WeatherForecast?$skip=3&$top=5

Unlike ASP.NET Core 5.0, you don’t need to write any formatters or anything for OData to work with Open API (Swagger).

Swagger enabled OData

Here few helpful links which talks about OData in ASP.NET Core.

  1. Up & Running with OData in ASP.NET 6
  2. API versioning extension with ASP.NET Core OData 8

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub