-
Notifications
You must be signed in to change notification settings - Fork 712
API Versioning with OData
Service API versioning using ASP.NET Web API and OData v4.0 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.
public class Startup
{
public void Configuration( IAppBuilder appBuilder )
{
var configuration = new HttpConfiguration();
var httpServer = new HttpServer( configuration );
configuration.AddApiVersioning();
var modelBuilder = new VersionedODataModelBuilder( configuration )
{
ModelConfigurations =
{
new PersonModelConfiguration()
}
};
var models = modelBuilder.GetEdmModels();
configuration.MapVersionedODataRoutes( "odata", "api", models );
appBuilder.UseWebApi( httpServer );
}
}
In ASP.NET Web API, OData defines route prefixes when the routes are mapped. To use a route prefix, you need to either explicitly build up an EDM for a specific API version
public class Startup
{
public void Configuration( IAppBuilder appBuilder )
{
var configuration = new HttpConfiguration();
var httpServer = new HttpServer( configuration );
configuration.AddApiVersioning();
var modelBuilder = new ODataConventionModelBuilder();
var modelConfigurations = new[] { new PersonModelConfiguration() };
var apiVersion = new ApiVersion( 1, 0 );
foreach ( var modelConfiguration in modelConfigurations )
{
modelConfiguration.Apply( modelBuilder, apiVersion );
}
var model = modelBuilder.GetEdmModel();
configuration.MapVersionedODataRoute( "odata-v1", "v1", model, apiVersion );
appBuilder.UseWebApi( httpServer );
}
}
or build the EDMs for all API versions and then extract the EDM for a specific API version.
public class Startup
{
public void Configuration( IAppBuilder appBuilder )
{
var configuration = new HttpConfiguration();
var httpServer = new HttpServer( configuration );
configuration.AddApiVersioning();
var modelBuilder = new VersionedODataModelBuilder( configuration )
{
ModelConfigurations =
{
new PersonModelConfiguration()
}
};
var models = modelBuilder.GetEdmModels();
var v1 = new ApiVersion( 1, 0 );
var modelV1 = models.Single( m => m.GetAnnotationValue<ApiVersionAnnotation>( m ).ApiVersion == v1 );
configuration.MapVersionedODataRoute( "odata-v1", "v1", modelV1, v1 );
appBuilder.UseWebApi( httpServer );
}
}
The remainder of the OData configuration and setup remains unchanged.
- Home
- Quick Starts
- Version Format
- Version Discovery
- Version Policies
- How to Version Your Service
- API Versioning with OData
- Configuring Your Application
- Error Responses
- API Documentation
- Extensions and Customizations
- Known Limitations
- FAQ
- Examples