Skip to content

API Versioning with OData

Chris Martinez edited this page Sep 14, 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-bypath", "v{apiVersion}", model, apiVersion );
        appBuilder.UseWebApi( httpServer );
    }
}

or build the EDMs for all API versions.

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.MapVersionedODataRoute( "odata-bypath", "v{apiVersion}", models );
        appBuilder.UseWebApi( httpServer );
    }
}

The remainder of the OData configuration and setup remains unchanged.

Clone this wiki locally