In today’s world, where videos have become increasingly shorter and gained immense popularity through platforms like YouTube Shorts, Insta Reels, and TikTok, why shouldn’t blog posts follow suit? I’m considering launching a series of brief blog posts that you can read and finish in less than one minute. Here is a very short post to add or enable OData in ASP.NET Core 7.0 application.
To add or enable OData to ASP.NET Core 7.0 application, follow these simple 3 steps.
1. Install the OData NuGet Package
Install the following Nuget package, either via package manager console,
Install-Package Microsoft.AspNetCore.OData
Or, via Nuget Package manager.
2. Register OData Service
Next, open program.cs and replace this,
builder.Services.AddControllers();
with this,
builder.Services.AddControllers().AddOData(options =>
options.Select().Filter().Count().OrderBy().Expand());
Here, we are enabling different OData operations like Select, Filter, Count, Expand and OrderBy. You can modify this as per your need.
3. EnableQuery Attribute
Lastly, put [EnableQuery] attribute on top of API method for which you want to enable OData.
[HttpGet]
[EnableQuery]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
That’s it.
Test your oData changes by appending OData query options. Like,
https://localhost:7103/weatherforecast?select=date,summary&$orderby=summary
One thought to “Shorts Series – Add or Enable OData in ASP.NET Core 7.0”