0

I am trying to get versioning to work via the request header in .NET 5 using Microsoft.AspNetCore.OData v8.0.1 and versioning to work with SwaggerUI.

Pre-v8, you used to be able to use

services.AddODataApiExplorer(...);

which would enable DependencyInjection for the Startup's Configure method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider) {
    ...
    app.UseSwagger();

    app.UseSwaggerUI(options => {
        // build a swagger endpoint for each discovered API version
        foreach (var description in provider.ApiVersionDescriptions) {
                    
            options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
        }
        options.ShowExtensions();
    });
    ...
}

OData8 appears to not handle this out of the box and the best documentation I can find on versioning only handles URL Segment and hints at Query String versioning. I'd prefer to save characters in the URL if I can which is why I want to go with Request Header versioning.

Any guidance is appreciated.

Flea
  • 1,490
  • 2
  • 19
  • 43
  • You can add a header parameter to all versioned actions using an operation filter. It would show up in the swagger ui. Would that work for you? – abdusco Jul 29 '21 at 10:33
  • Like this https://stackoverflow.com/questions/68101038/custom-operation-ids-for-single-document-and-not-the-other/68176836#68176836 but as a header, not query parameter. See the header `Setting default parameters depending on version in Swagger UI` – abdusco Jul 29 '21 at 10:34

2 Answers2

0

So, instead of using

services.AddODataApiExplorer(...);

you can use

services.AddApiVersioning(...);

It won't (currently) get picked up properly by the SwaggerUI but it will generate the client code properly and respond to versioning by routing to the target versioned controller.

I was able to find the most clear and concise documentation on versioning in the article, 'REST API versioning with ASP.NET Core'

Flea
  • 1,490
  • 2
  • 19
  • 43
0

//WHEN VERSIONING BY: query string, header, or media type

  endpoints.MapVersionedODataRoute( "odata", "api", modelBuilder );

//WHEN VERSIONING BY: url segment

endpoints.MapVersionedODataRoute( "odata-bypath", "api/v{version:apiVersion}", modelBuilder );
Hotkey
  • 103
  • 7
  • Also, take a look this article https://dev.to/99darshan/restful-web-api-versioning-with-asp-net-core-1e8g – Hotkey Sep 08 '21 at 19:20