OData is a set of best practices for building and consuming RESTful APIs. ASP.NET Core OData Nuget package provides capabilities for smooth development of OData REST API in .NET application.
I would like to document how I added OData support via the command line since .NET 6.0 includes new structure of Program.cs and I've faced couple challenges there.
Here are the steps that I followed:
Create ASP.NET Core Web API 6.0 application
>> dotnet new webapi -o odata
>> cd odata
Add Entity Framework Core and OData package references
I used EF Core In-Memory Database Provider for simplicity.
>> dotnet add package Microsoft.EntityFrameworkCore.InMemory
>> dotnet add package Microsoft.AspNetCore.Odata
Update Program.cs
- OData convention model
- OData controller
- Entity Framework Context
using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using OData.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
// Entity Framework Context
builder.Services.AddDbContext<ODataContext>(opt => opt.UseInMemoryDatabase("Cars"));
// OData convention model and OData controller
builder.Services.AddControllers().AddOData(opt => opt.AddRouteComponents("odata", GetEdmModel()).Filter().Select());
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Car>("Cars");
return builder.GetEdmModel();
}
Add support for GET requests
We will use EnableQuery
annotation to enable GET Requests CarsController.cs
. We use a static class DataSource
provides the test data. The code for DataSource.GetCars()
can be found here
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using OData.Models;
using OData.Data;
namespace OData.Controllers
{
public class CarsController : ODataController
{
private ODataContext _context;
private readonly ILogger<CarsController> _logger;
public CarsController(ODataContext context, ILogger<CarsController> logger) {
_context = context;
_logger = logger;
if (_context.Cars.Count() == 0)
{
foreach (var b in DataSource.GetCars())
{
_context.Cars.Add(b);
}
_context.SaveChanges();
}
}
[EnableQuery]
public IActionResult Get()
{
return Ok(_context.Cars);
}
[EnableQuery]
public IActionResult Get(int key)
{
return Ok(_context.Cars.FirstOrDefault(c => c.Id == key));
}
}
}
Run the application and check the result
Good way to prove that OData works is to query all cars where the price is less or equal 50. There is only one car that satisfies that condition.
>> dotnet run
>> curl -X 'GET' 'https://localhost:7005/odata/Cars?$filter=Price le 50' -H 'accept: text/plain' -k
The response should look like the following:
{
"@odata.context": "https://localhost:7005/odata/$metadata#Cars",
"value": [
{
"Id": 2,
"Brand": "BMW",
"Price": 49.99,
"Model": "3 Series"
}
]
}
Summary
In this example, we looked at adding support for OData to ASP.NET Core Web API application and performed a simple query to prove that the OData requests are coming through. Hope this helped you run your project.
Top comments (2)
The information you give us in this article is very helpful. I enjoyed reading your post, it helped me to know a lot more. phoodle Let's keep doing this!
Ótimo artigo muito importante e informativo pra os tempos atuais, conhecimento é muito importante ainda mais hoje em dia então aproveite pra ler livros grátis, e aprender muito mais sobre o assunto encontre os melhores livros sobre o tema do artigo.