From 4d57ce1e2dfe0d9e568d6d713dd3ba668609da2b Mon Sep 17 00:00:00 2001 From: James Croft Date: Sun, 30 Jan 2022 19:43:57 +0000 Subject: [PATCH] Added API versioning extenions for .NET 5 applications --- .../Extensions/ApiVersioningExtensions.cs | 61 +++++++++++++++++++ src/MADE.Web/MADE.Web.csproj | 4 ++ 2 files changed, 65 insertions(+) create mode 100644 src/MADE.Web/Extensions/ApiVersioningExtensions.cs diff --git a/src/MADE.Web/Extensions/ApiVersioningExtensions.cs b/src/MADE.Web/Extensions/ApiVersioningExtensions.cs new file mode 100644 index 00000000..23e8e6b2 --- /dev/null +++ b/src/MADE.Web/Extensions/ApiVersioningExtensions.cs @@ -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; + + /// + /// Defines a collection of extensions for API versioning. + /// + public static class ApiVersioningExtensions + { + /// + /// Adds request API versioning for controllers and APIs to the specified services collection. + /// + /// The services available in the application. + /// The default major version of the API. Default, 1. + /// The default minor version of the API. Default, 0. + /// The configured object. + 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; + } + + /// + /// Adds request header API versioning for controllers and APIs to the specified services collection. + /// + /// The services available in the application. + /// The name of the header that is required when making requests to API endpoints. Default, x-api-version. + /// The default major version of the API. Default, 1. + /// The default minor version of the API. Default, 0. + /// The configured object. + 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 \ No newline at end of file diff --git a/src/MADE.Web/MADE.Web.csproj b/src/MADE.Web/MADE.Web.csproj index 2aa7da51..f54eb6c0 100644 --- a/src/MADE.Web/MADE.Web.csproj +++ b/src/MADE.Web/MADE.Web.csproj @@ -22,4 +22,8 @@ + + + + \ No newline at end of file