File tree Expand file tree Collapse file tree 8 files changed +53
-33
lines changed
Expand file tree Collapse file tree 8 files changed +53
-33
lines changed Original file line number Diff line number Diff line change 99 <ItemGroup >
1010 <PackageReference Include =" Asp.Versioning.Mvc" Version =" 8.0.0" />
1111 <PackageReference Include =" Asp.Versioning.Mvc.ApiExplorer" Version =" 8.0.0" />
12- <PackageReference Include =" Microsoft.AspNetCore.OpenApi" Version =" 8.0.2 " />
13- <PackageReference Include =" Microsoft.EntityFrameworkCore.Design" Version =" 8.0.2 " >
12+ <PackageReference Include =" Microsoft.AspNetCore.OpenApi" Version =" 8.0.3 " />
13+ <PackageReference Include =" Microsoft.EntityFrameworkCore.Design" Version =" 8.0.3 " >
1414 <IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
1515 <PrivateAssets >all</PrivateAssets >
1616 </PackageReference >
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ public class ApiConstants
55 public const string ApplicationName = "DummyWebApiDemo" ;
66
77 public const string VersionHeaderName = "X-Api-Version" ;
8+
9+ public const string GroupNameFormat = "'v'VVV" ;
810
911 public static class Versions
1012 {
Original file line number Diff line number Diff line change 11using System . Reflection ;
22using Api . Extensions ;
3- using Asp . Versioning ;
43using Microsoft . AspNetCore . HttpLogging ;
54using Serilog ;
65using Serilog . Debugging ;
@@ -25,29 +24,6 @@ public static IServiceCollection AddApi(this WebApplicationBuilder builder)
2524 return services ;
2625 }
2726
28- private static void AddVersioning ( this IServiceCollection services )
29- {
30- const string name = Constants . ApiConstants . VersionHeaderName ;
31- var defaultVersion = new ApiVersion ( 1 , 0 ) ;
32- services
33- . AddApiVersioning ( options =>
34- {
35- options . ReportApiVersions = true ;
36- options . DefaultApiVersion = defaultVersion ;
37- options . AssumeDefaultVersionWhenUnspecified = true ;
38- options . ApiVersionReader = ApiVersionReader . Combine (
39- new UrlSegmentApiVersionReader ( ) ,
40- new HeaderApiVersionReader ( name ) ,
41- new MediaTypeApiVersionReader ( name ) ) ;
42- } ) . AddApiExplorer ( options =>
43- {
44- options . GroupNameFormat = "'v'VVV" ;
45- options . SubstituteApiVersionInUrl = true ;
46- options . DefaultApiVersion = defaultVersion ;
47- options . AssumeDefaultVersionWhenUnspecified = true ;
48- } ) ;
49- }
50-
5127 private static void AddHttpLogging ( this IServiceCollection services )
5228 {
5329 services . AddHttpLogging ( logging =>
Original file line number Diff line number Diff line change @@ -30,10 +30,12 @@ public static void UseSwaggerDoc(this WebApplication app)
3030 app . UseSwaggerUI ( options =>
3131 {
3232 options . DisplayRequestDuration ( ) ;
33- var apiVersionDescriptionProvider = app . Services . GetRequiredService < IApiVersionDescriptionProvider > ( ) ;
34- foreach ( var description in apiVersionDescriptionProvider . ApiVersionDescriptions )
33+ var descriptions = app . DescribeApiVersions ( ) ;
34+ foreach ( var description in descriptions )
3535 {
36- options . SwaggerEndpoint ( $ "/swagger/{ description . GroupName } /swagger.json", description . GroupName . ToUpper ( ) ) ;
36+ var url = $ "/swagger/{ description . GroupName } /swagger.json";
37+ var name = description . GroupName ;
38+ options . SwaggerEndpoint ( url , name ) ;
3739 }
3840 } ) ;
3941 }
Original file line number Diff line number Diff line change 1+ using Asp . Versioning ;
2+ using static Api . Constants . ApiConstants ;
3+
4+ namespace Api . Extensions ;
5+
6+ public static class VersioningExtensions
7+ {
8+ public static IServiceCollection AddVersioning ( this IServiceCollection services )
9+ {
10+ var defaultVersion = new ApiVersion ( 1 , 0 ) ;
11+
12+ services
13+ . AddApiVersioning ( options =>
14+ {
15+ options . ReportApiVersions = true ;
16+ options . AssumeDefaultVersionWhenUnspecified = true ;
17+ options . DefaultApiVersion = defaultVersion ;
18+ options . ApiVersionReader = GetApiVersionReader ( ) ;
19+ } )
20+ . AddApiExplorer ( options =>
21+ {
22+ options . GroupNameFormat = GroupNameFormat ;
23+ options . SubstituteApiVersionInUrl = true ;
24+ options . AssumeDefaultVersionWhenUnspecified = true ;
25+ options . DefaultApiVersion = defaultVersion ;
26+ options . ApiVersionParameterSource = GetApiVersionReader ( ) ;
27+ } ) ;
28+
29+ return services ;
30+ }
31+
32+ private static IApiVersionReader GetApiVersionReader ( )
33+ {
34+ return ApiVersionReader . Combine (
35+ new UrlSegmentApiVersionReader ( ) ,
36+ new QueryStringApiVersionReader ( VersionHeaderName ) ,
37+ new HeaderApiVersionReader ( VersionHeaderName ) ,
38+ new MediaTypeApiVersionReader ( VersionHeaderName ) ) ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1515 </ItemGroup >
1616
1717 <ItemGroup >
18- <PackageReference Include =" Microsoft.EntityFrameworkCore.Sqlite" Version =" 8.0.2 " />
18+ <PackageReference Include =" Microsoft.EntityFrameworkCore.Sqlite" Version =" 8.0.3 " />
1919 </ItemGroup >
2020
2121</Project >
Original file line number Diff line number Diff line change 99 </PropertyGroup >
1010
1111 <ItemGroup >
12- <PackageReference Include =" coverlet.collector" Version =" 6.0.1 " >
12+ <PackageReference Include =" coverlet.collector" Version =" 6.0.2 " >
1313 <IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
1414 <PrivateAssets >all</PrivateAssets >
1515 </PackageReference >
1616 <PackageReference Include =" FluentAssertions" Version =" 6.12.0" />
17- <PackageReference Include =" Microsoft.AspNetCore.Mvc.Testing" Version =" 8.0.2 " />
17+ <PackageReference Include =" Microsoft.AspNetCore.Mvc.Testing" Version =" 8.0.3 " />
1818 <PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" 17.9.0" />
1919 <PackageReference Include =" xunit" Version =" 2.7.0" />
2020 <PackageReference Include =" xunit.runner.visualstudio" Version =" 2.5.7" >
Original file line number Diff line number Diff line change 1010
1111 <ItemGroup >
1212 <PackageReference Include =" AutoFixture" Version =" 4.18.1" />
13- <PackageReference Include =" coverlet.collector" Version =" 6.0.1 " >
13+ <PackageReference Include =" coverlet.collector" Version =" 6.0.2 " >
1414 <IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
1515 <PrivateAssets >all</PrivateAssets >
1616 </PackageReference >
You can’t perform that action at this time.
0 commit comments