Skip to content

API Versioning with OData

Chris Martinez edited this page Jul 29, 2016 · 9 revisions

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.

ASP.NET Web API and OData v4.0 with OWIN

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.

Clone this wiki locally