BetterVersioning.net 2.0.1

dotnet add package BetterVersioning.net --version 2.0.1
                    
NuGet\Install-Package BetterVersioning.net -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BetterVersioning.net" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BetterVersioning.net" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="BetterVersioning.net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BetterVersioning.net --version 2.0.1
                    
#r "nuget: BetterVersioning.net, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BetterVersioning.net@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BetterVersioning.net&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=BetterVersioning.net&version=2.0.1
                    
Install as a Cake Tool

Betterversioning.net

Build Status Nuget Maintainability Rating Security Rating Vulnerabilities https://img.shields.io/badge/view_on-Github-blue

BetterVersioning.net is an opinionated convention for the versioning of Web APIs. It allows you to add versions in a "from & until" manner instead of an attribute for every version. See the Using it chapter.

For a complete example see the example folder.

Explanation

BetterVersioning.net works by adding API versions in a from & until manner instead of manually specifying every version on a controller/method.

Specifying [From(1,0)] would mean this controller/method is available in every version that you have added to BetterVersioning.net that is version 1.0 or higher.

Adding an [Until(5,0)] to the same controller/method will ensure that the controller/method will no longer show up on (or after) version 5.0. (depending on options)

Why

Managing multiple versions of an API with the regular [ApiVersion(1.0)] and [MapToApiVersion("1.0")] attributes can become cumbersome as the number of versions inside of your API increase.

The main reasons I initially came up with this versioning idea are:

  • No need to modify controllers that are not changed when you add a new version
  • Versions are automatically added/removed from controllers based on a convention
  • Simplify multiple supported/deprecated versions

Installation

dotnet add package BetterVersioning.net

Using it

  1. Define your API versions in the following format:

      var versions = new[] {
        new BetterVersion(32, new ushort[]{1,2}, supported: false),
        new BetterVersion(33, supported: false)
      };
    
  2. Modify the builder.Services.AddApiVersioning block by adding the BetterVersioning convention:

    builder.Services.AddApiVersioning(opt =>
    {
        opt.AssumeDefaultVersionWhenUnspecified = true;
        opt.ReportApiVersions = true;
        opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
                                                        new HeaderApiVersionReader("x-api-version"),
                                                        new MediaTypeApiVersionReader("x-api-version"));
    
        opt.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(32, 0);
    
        // set up versions
        var versions = new[] {
            new BetterVersion(31, new ushort[]{1,2,3}),
            new BetterVersion(32),
        };
    
        // Add the convention
        opt.Conventions = new BetterVersioningConventionBuilder(versions, new BetterVersioningOptions() { UntilInclusive = true });
    });
    
  3. Add a [From] and/or [Until] attribute to your controller or method. The code below shows several examples.

      [ApiController]
      [Route("v{version:apiVersion}/[controller]")]
      [From(6)]
      public class BetterVersionsController : ControllerBase
      {
    
          [HttpGet]
          public string Get1() => "This endpoint was introduced in version 6";
    
          [HttpGet("minor-range")]
          [Until(6, 1)]
          public string Get2() => "This endpoint was introduced in version 6.0 and removed after version 6.1";
    
          [HttpGet("new")]
          [From(31)]
          [Until(33)]
          public string Get3() => "This endpoint was introduced in version 31 and removed after version 33";
    
          [HttpGet("new-only-minors")]
          [From(34, 1)]
          [Until(34, 2)]
          public string Get4() => "This endpoint was introduced in version 34.1 and removed after version 34.2";
    
          [HttpGet("new-till-31")]
          [Until(31)]
          public string Get5() => "This endpoint was introduced in version 6 and removed after version 31.0";
      }
    
    

Defining versions

The main parameter for the convention is the "versions" array. This array can be declared wherever you want and consists of BetterVersion objects.

Each object is constructed with 3 main parameters:

Name Type Description
MajorVersion ushort The major version for this version
MinorVersions array of ushort The minor versions you want included in this version
Supported boolean Whether the version is still supported (false would mean deprecated)

All unsupported (/deprecated) versions are still usable but will get the deprecation message (if set up in OpenApi) applied.

Convention options

When you add the convention you can (optionally) pass an options object that contains configuration options for the convention. All options are listed in the table below.

Name Type Description
UntilInclusive boolean Whether the [Until] attribute is inclusive or exclusive of the given version number.
DetectDuplicatesAtStartup boolean Whether BetterVersioning.net checks for, and errors out if, duplicates when it starts
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 220 8/28/2024
2.0.0 223 8/19/2024
1.0.4 5,719 8/19/2024
1.0.3 24,339 7/16/2023
1.0.2 233 7/16/2023
1.0.1 233 7/16/2023
1.0.0 258 7/16/2023