Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Attributes

In addition to the API versioning options, there are few other customization and extension points. Attributes are the primary mechanism used to decorate the API version metadata with a specific controller type, but the attributes used can be any IApiVersionProvider.

public interface IApiVersionProvider
{
    ApiVersionProviderOptions Options { get; }
    IReadOnlyList<ApiVersion> Versions { get; }
}

There are several API version provider attributes defined out-of-the-box:

  • ApiVersionsBaseAttribute
  • ApiVersionAttribute
  • MapToApiVersionAttribute
  • AdvertiseApiVersionsAttribute

These attributes are themselves extensible. For example, you might choose to have your own attributes that are unambiguously a specific version:

[AttributeUsage( AttributeUsage.Class, AllowMultiple = true, Inherited = false )]
public sealed class V1Attribute : ApiVersionAttribute
{
    public V1Attribute() : base( new ApiVersion( new( 2016, 7, 1 ) ) ) { }
}
[V1]
[ApiController]
[Route( "api/[controller]" )]
public class HelloWorldController : ControllerBase
{
    [HttpGet]
    public string Get() => "Hello world!";
}

This approach can help centralize API version management and avoid developer typographical errors when implementing a set of services that all use the same API version.