13
13
using Microsoft . Extensions . DependencyInjection ;
14
14
using Microsoft . Extensions . Hosting ;
15
15
using Microsoft . Extensions . Logging ;
16
+ using Microsoft . Extensions . Options ;
16
17
using Microsoft . OpenApi . Models ;
17
18
using SampleWebApiAspNetCore . MappingProfiles ;
18
19
using SampleWebApiAspNetCore . Repositories ;
19
20
using SampleWebApiAspNetCore . Services ;
21
+ using Swashbuckle . AspNetCore . SwaggerGen ;
20
22
21
- namespace WebApplication11
23
+ namespace SampleWebApiAspNetCore
22
24
{
23
25
public class Startup
24
26
{
@@ -47,7 +49,7 @@ public void ConfigureServices(IServiceCollection services)
47
49
} ) ;
48
50
49
51
services . AddSingleton < ISeedDataService , SeedDataService > ( ) ;
50
- services . AddScoped < IFoodRepository , EfFoodRepository > ( ) ;
52
+ services . AddScoped < IFoodRepository , FoodSqlRepository > ( ) ;
51
53
services . AddRouting ( options => options . LowercaseUrls = true ) ;
52
54
services . AddSingleton < IActionContextAccessor , ActionContextAccessor > ( ) ;
53
55
services . AddScoped < IUrlHelper > ( x =>
@@ -62,32 +64,25 @@ public void ConfigureServices(IServiceCollection services)
62
64
. AddNewtonsoftJson ( )
63
65
. SetCompatibilityVersion ( CompatibilityVersion . Version_3_0 ) ;
64
66
65
- services . AddApiVersioning ( config =>
66
- {
67
- config . ReportApiVersions = true ;
68
- config . AssumeDefaultVersionWhenUnspecified = true ;
69
- config . DefaultApiVersion = new ApiVersion ( 1 , 0 ) ;
70
- config . ApiVersionReader = new HeaderApiVersionReader ( "api-version" ) ;
71
- } ) ;
72
-
73
- services . AddVersionedApiExplorer ( o => o . GroupNameFormat = "'v'VVV" ) ;
74
- services . AddSwaggerGen (
75
- options =>
76
- {
77
- ServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
78
- var provider = serviceProvider . GetRequiredService < IApiVersionDescriptionProvider > ( ) ;
67
+ services . AddApiVersioning (
68
+ config =>
69
+ {
70
+ config . ReportApiVersions = true ;
71
+ config . AssumeDefaultVersionWhenUnspecified = true ;
72
+ config . DefaultApiVersion = new ApiVersion ( 1 , 0 ) ;
73
+ config . ApiVersionReader = new HeaderApiVersionReader ( "api-version" ) ;
74
+ } ) ;
75
+ services . AddVersionedApiExplorer (
76
+ options =>
77
+ {
78
+ options . GroupNameFormat = "'v'VVV" ;
79
79
80
- foreach ( var description in provider . ApiVersionDescriptions )
81
- {
82
- options . SwaggerDoc (
83
- description . GroupName ,
84
- new OpenApiInfo ( )
85
- {
86
- Title = $ "Sample API { description . ApiVersion } ",
87
- Version = description . ApiVersion . ToString ( )
88
- } ) ;
89
- }
90
- } ) ;
80
+ // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
81
+ // can also be used to control the format of the API version in route templates
82
+ options . SubstituteApiVersionInUrl = true ;
83
+ } ) ;
84
+ services . AddTransient < IConfigureOptions < SwaggerGenOptions > , ConfigureSwaggerOptions > ( ) ;
85
+ services . AddSwaggerGen ( ) ;
91
86
92
87
services . AddAutoMapper ( typeof ( FoodMappings ) ) ;
93
88
}
0 commit comments