Skip to content

API Documentation

Chris Martinez edited this page Jun 12, 2017 · 19 revisions

Overview

Adding documentation is often the final, pivotal step in making your versioned services available to clients and fosters their utilization. While there are many approaches to documenting your services, Swagger has quickly become the de facto method for describing REST services. The most popular way to provide Swagger documentation for REST services implemented ASP.NET is using Swashbuckle.

The ASP.NET API versioning project provides several new API explorer implementations that make it easy to add versioning into your Swagger and Swashbuckle configurations. Each of these API explorers do all of the heavy lifting to discover and collate your REST services by API version. They do not directly rely on nor use Swagger or Swashbuckle so that you can use them for other scenarios as well.

Everything you need to add versioned documentation to your ApiController services.

configuration.AddApiVersioning();

// format the version as "'v'major[.minor][-status]"
var apiExplorer = configuration.AddVersionedApiExplorer( o => o.GroupNameFormat = "'v'VVV" );

configuration.EnableSwagger(
    "{apiVersion}/swagger",
    swagger =>
    {
        swagger.MultipleApiVersions(
            ( apiDescription, version ) => apiDescription.GetGroupName() == version,
            info =>
            {
                foreach ( var group in apiExplorer.ApiDescriptions )
                {
                    info.Version( group.Name, $"Sample API {group.ApiVersion}" );
                }
            } );
    } )
 .EnableSwaggerUi( swagger => swagger.EnableDiscoveryUrlSelector() );

Review the sample project for additional setup and configuration options.

Everything you need to add versioned documentation to your ODataController services.

configuration.AddApiVersioning();

var modelBuilder = new VersionedODataModelBuilder( configuration )
{
    ModelBuilderFactory = () => new ODataConventionModelBuilder().EnableLowerCamelCase(),
    ModelConfigurations = { new MyModelConfiguration() }
};
var models = modelBuilder.GetEdmModels();

configuration.MapVersionedODataRoutes( "odata", "api", models );

// format the version as "'v'major[.minor][-status]"
var apiExplorer = configuration.AddODataApiExplorer( o => o.GroupNameFormat = "'v'VVV" );

configuration.EnableSwagger(
    "{apiVersion}/swagger",
    swagger =>
    {
        swagger.MultipleApiVersions(
            ( apiDescription, version ) => apiDescription.GetGroupName() == version,
            info =>
            {
                foreach ( var group in apiExplorer.ApiDescriptions )
                {
                    info.Version( group.Name, $"Sample API {group.ApiVersion}" );
                }
            } );
    } )
 .EnableSwaggerUi( swagger => swagger.EnableDiscoveryUrlSelector() );

Review the sample project for additional setup and configuration options.

Note: This API explorer does not directly tie into the Swashbuckle with OData support because that project also prescribes how API versioning is performed, which is incompatible with this project. This may change or improve in the future.

Everything you need to add versioned documentation to your Controller services.

public void ConfigureServices( IServiceCollection services )
{
    // format the version as "'v'major[.minor][-status]"
    services.AddMvcCore().AddVersionedApiExplorer( o => o.GroupNameFormat = "'v'VVV" );
    services.AddMvc();
    services.AddApiVersioning();
    services.AddSwaggerGen(
        options =>
        {
            var provider = services.BuildServiceProvider()
                                   .GetRequiredService<IApiVersionDescriptionProvider>();

            foreach ( var description in provider.ApiVersionDescriptions )
            {
                options.SwaggerDoc(
                    description.GroupName,
                    new Info()
                    {
                        Title = $"Sample API {description.ApiVersion}",
                        Version = description.ApiVersion.ToString()
                    } );
            }
        } );
}

public void Configure(
    IApplicationBuilder app,
    IHostingEnvironment env,
    ILoggerFactory loggerFactory,
    IApiVersionDescriptionProvider provider )
{
    app.UseMvc();
    app.UseApiVersioning(); // required in 1.1+
    app.UseSwagger();
    app.UseSwaggerUI(
        options =>
        {
            foreach ( var description in provider.ApiVersionDescriptions )
            {
                options.SwaggerEndpoint(
                    $"/swagger/{description.GroupName}/swagger.json",
                    description.GroupName.ToUpperInvariant() );
            }
        } );
}

Review the sample project for additional setup and configuration options.

Clone this wiki locally