| | | 1 | | using Asp.Versioning; |
| | | 2 | | using ProjectTemplate.Web.Options; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods to register API versioning services for the application. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class ApiVersioningServiceExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Adds API versioning services and conventions for controller-based APIs. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="services">The service collection to configure.</param> |
| | | 15 | | /// <param name="configuration">The application configuration source.</param> |
| | | 16 | | /// <returns>The same service collection instance so calls can be chained.</returns> |
| | | 17 | | public static IServiceCollection AddApplicationApiVersioning( |
| | | 18 | | this IServiceCollection services, |
| | | 19 | | IConfiguration configuration) |
| | | 20 | | { |
| | 162 | 21 | | ApplicationApiVersioningOptions apiVersioningOptions = new(); |
| | | 22 | | |
| | 162 | 23 | | configuration |
| | 162 | 24 | | .GetSection(ApplicationApiVersioningOptions.SectionName) |
| | 162 | 25 | | .Bind(apiVersioningOptions); |
| | | 26 | | |
| | 162 | 27 | | ValidateApiVersioningOptions(apiVersioningOptions); |
| | | 28 | | |
| | 154 | 29 | | services |
| | 154 | 30 | | .AddOptions<ApplicationApiVersioningOptions>() |
| | 154 | 31 | | .Bind(configuration.GetSection(ApplicationApiVersioningOptions.SectionName)) |
| | 130 | 32 | | .Validate(options => options.DefaultMajorVersion > 0, |
| | 154 | 33 | | "ProjectTemplate:ApiVersioning:DefaultMajorVersion must be greater than zero.") |
| | 130 | 34 | | .Validate(options => options.DefaultMinorVersion >= 0, |
| | 154 | 35 | | "ProjectTemplate:ApiVersioning:DefaultMinorVersion must be zero or greater.") |
| | 130 | 36 | | .Validate(options => options.EnableUrlSegmentVersioning || options.EnableHeaderVersioning, |
| | 154 | 37 | | "ProjectTemplate:ApiVersioning must enable URL segment versioning, header versioning, or both.") |
| | 130 | 38 | | .Validate(options => !options.EnableHeaderVersioning || !string.IsNullOrWhiteSpace(options.HeaderName), |
| | 154 | 39 | | "ProjectTemplate:ApiVersioning:HeaderName is required when header versioning is enabled.") |
| | 154 | 40 | | .ValidateOnStart(); |
| | | 41 | | |
| | 154 | 42 | | IApiVersionReader[] readers = CreateApiVersionReaders(apiVersioningOptions); |
| | | 43 | | |
| | 154 | 44 | | services |
| | 154 | 45 | | .AddApiVersioning(options => |
| | 154 | 46 | | { |
| | 142 | 47 | | options.DefaultApiVersion = new ApiVersion( |
| | 142 | 48 | | apiVersioningOptions.DefaultMajorVersion, |
| | 142 | 49 | | apiVersioningOptions.DefaultMinorVersion); |
| | 154 | 50 | | |
| | 142 | 51 | | options.AssumeDefaultVersionWhenUnspecified = |
| | 142 | 52 | | apiVersioningOptions.AssumeDefaultVersionWhenUnspecified; |
| | 154 | 53 | | |
| | 142 | 54 | | options.ReportApiVersions = apiVersioningOptions.ReportApiVersions; |
| | 142 | 55 | | options.ApiVersionReader = ApiVersionReader.Combine(readers); |
| | 142 | 56 | | }) |
| | 154 | 57 | | .AddMvc(); |
| | | 58 | | |
| | 154 | 59 | | return services; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private static IApiVersionReader[] CreateApiVersionReaders( |
| | | 63 | | ApplicationApiVersioningOptions options) |
| | | 64 | | { |
| | 154 | 65 | | List<IApiVersionReader> readers = []; |
| | | 66 | | |
| | 154 | 67 | | if (options.EnableUrlSegmentVersioning) |
| | | 68 | | { |
| | 152 | 69 | | readers.Add(new UrlSegmentApiVersionReader()); |
| | | 70 | | } |
| | | 71 | | |
| | 154 | 72 | | if (options.EnableHeaderVersioning) |
| | | 73 | | { |
| | 152 | 74 | | readers.Add(new HeaderApiVersionReader(options.HeaderName)); |
| | | 75 | | } |
| | | 76 | | |
| | 154 | 77 | | return [.. readers]; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private static void ValidateApiVersioningOptions(ApplicationApiVersioningOptions options) |
| | | 81 | | { |
| | 162 | 82 | | if (options.DefaultMajorVersion <= 0) |
| | | 83 | | { |
| | 2 | 84 | | throw new InvalidOperationException( |
| | 2 | 85 | | "ProjectTemplate:ApiVersioning:DefaultMajorVersion must be greater than zero."); |
| | | 86 | | } |
| | | 87 | | |
| | 160 | 88 | | if (options.DefaultMinorVersion < 0) |
| | | 89 | | { |
| | 2 | 90 | | throw new InvalidOperationException( |
| | 2 | 91 | | "ProjectTemplate:ApiVersioning:DefaultMinorVersion must be zero or greater."); |
| | | 92 | | } |
| | | 93 | | |
| | 158 | 94 | | if (!options.EnableUrlSegmentVersioning && !options.EnableHeaderVersioning) |
| | | 95 | | { |
| | 2 | 96 | | throw new InvalidOperationException( |
| | 2 | 97 | | "ProjectTemplate:ApiVersioning must enable URL segment versioning, header versioning, or both."); |
| | | 98 | | } |
| | | 99 | | |
| | 156 | 100 | | if (options.EnableHeaderVersioning && string.IsNullOrWhiteSpace(options.HeaderName)) |
| | | 101 | | { |
| | 2 | 102 | | throw new InvalidOperationException( |
| | 2 | 103 | | "ProjectTemplate:ApiVersioning:HeaderName is required when header versioning is enabled."); |
| | | 104 | | } |
| | 154 | 105 | | } |
| | | 106 | | } |