Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

API Versioning with OData

Service API versioning using OData is similar to the normal configuration with a few slight variations. Each implemented OData controller has an associated entity set and each entity set is defined in an Entity Data Model (EDM). Once we introduce API versioning, each versioned OData controller now needs an EDM per API version. To satisfy this requirement, we’ll use the new VersionedODataModelBuilder, build a collection of EDMs for each API version, and then map a set of routes for them.

var builder = WebApplication.CreateBuilder( args );

builder.Services.AddControllers().AddOData();
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning()
                .AddOData( options => options.AddRouteComponents( "api" ) );

var app = builder.Build();

app.MapControllers();
app.Run();

It is possible to imperatively use:

.AddOData( options => options.ModelConfigurations.Add( new PersonModelConfiguration() ) )

however, it is typically unnecessary because this will automatically happen via dependency injection.

Important

Calling AddControllers().AddOData( options => options.AddRouteComponents( ... ) ) will be completely ignored by API Versioning. Due to the OData design, it is impossible to extend or customize this behavior. Instead, you need to use AddApiVersioning().AddOData( options => options.AddRouteComponents( ... ) ). The standard AddOData configuration can still be used to configure global query option settings.