Simplest approach to create OData controller and run queries with latest OData 8.x in .NET 5 and .NET 6.
I couldn't find bare bones example, so here it is.
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.0-rc" />
services.AddOData(opt =>
{
// specify EDM with "odata" prefix path
opt.AddModel("odata", EdmModelBuilder.GetEdmModel();
// enable features for OData
opt.Filter().Select().OrderBy().SetMaxTop(50).Count();
});
EDM is used to describe exposed data in the OData (resources, their properties and relations).
Each resource should have key defined. By default, property with name Id
is taken, but one can use [Key] attribute with other property. Check Bicycle model for example.
This is sample setup of EDM:
internal static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
// "Bicycles" will be used as a OData resource path
builder.EntitySet<Bicycle>("Bicycles");
return builder.GetEdmModel();
}
...
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
public class BicyclesController : ODataController
{
// enable OData queries for this endpoint
[EnableQuery()]
public ActionResult<IEnumerable<Bicycle>> Get()
{
return Ok(_bicycleRepository.GetBicycles());
}
}
Get top 5 lightest bikes
/odata/Bicycles?$top=5&$orderby=Weight
Get Red bicycles only
/odata/Bicycles?$filter=Color eq 'Red'
Get bicycles lighter or equal 9 (kg)
/odata/Bicycles?$filter=Weight le 9
- OData official repo
- OData nuget package
- OData documentation
- Routing for OData 8
- EnableQuery Attribute - applies to OData 7.x, but most Properties are suitable for OData 8