Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/MADE.Web/Extensions/ApiVersioningExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// MADE Apps licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NET5_0
namespace MADE.Web.Extensions
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Defines a collection of extensions for API versioning.
/// </summary>
public static class ApiVersioningExtensions
{
/// <summary>
/// Adds request API versioning for controllers and APIs to the specified services collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection">services</see> available in the application.</param>
/// <param name="defaultMajor">The default major version of the API. Default, 1.</param>
/// <param name="defaultMinor">The default minor version of the API. Default, 0.</param>
/// <returns>The configured <paramref name="services"/> object.</returns>
public static IServiceCollection AddApiVersionSupport(this IServiceCollection services, int defaultMajor = 1, int defaultMinor = 0)
{
services.AddVersionedApiExplorer(options => options.GroupNameFormat = "'v'VVV");

services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(defaultMajor, defaultMinor);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});

return services;
}

/// <summary>
/// Adds request header API versioning for controllers and APIs to the specified services collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection">services</see> available in the application.</param>
/// <param name="apiHeaderName">The name of the header that is required when making requests to API endpoints. Default, x-api-version.</param>
/// <param name="defaultMajor">The default major version of the API. Default, 1.</param>
/// <param name="defaultMinor">The default minor version of the API. Default, 0.</param>
/// <returns>The configured <paramref name="services"/> object.</returns>
public static IServiceCollection AddApiVersionHeaderSupport(this IServiceCollection services, string apiHeaderName = "x-api-version", int defaultMajor = 1, int defaultMinor = 0)
{
services.AddVersionedApiExplorer(options => options.GroupNameFormat = "'v'VVV");

services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(defaultMajor, defaultMinor);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
options.ApiVersionReader = new HeaderApiVersionReader(apiHeaderName);
});

return services;
}
}
}
#endif
4 changes: 4 additions & 0 deletions src/MADE.Web/MADE.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net5.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
</ItemGroup>
</Project>