1313using Microsoft . Extensions . DependencyInjection ;
1414using Microsoft . Extensions . Hosting ;
1515using Microsoft . Extensions . Logging ;
16+ using Microsoft . Extensions . Options ;
1617using Microsoft . OpenApi . Models ;
1718using SampleWebApiAspNetCore . MappingProfiles ;
1819using SampleWebApiAspNetCore . Repositories ;
1920using SampleWebApiAspNetCore . Services ;
21+ using Swashbuckle . AspNetCore . SwaggerGen ;
2022
21- namespace WebApplication11
23+ namespace SampleWebApiAspNetCore
2224{
2325 public class Startup
2426 {
@@ -47,7 +49,7 @@ public void ConfigureServices(IServiceCollection services)
4749 } ) ;
4850
4951 services . AddSingleton < ISeedDataService , SeedDataService > ( ) ;
50- services . AddScoped < IFoodRepository , EfFoodRepository > ( ) ;
52+ services . AddScoped < IFoodRepository , FoodSqlRepository > ( ) ;
5153 services . AddRouting ( options => options . LowercaseUrls = true ) ;
5254 services . AddSingleton < IActionContextAccessor , ActionContextAccessor > ( ) ;
5355 services . AddScoped < IUrlHelper > ( x =>
@@ -62,32 +64,25 @@ public void ConfigureServices(IServiceCollection services)
6264 . AddNewtonsoftJson ( )
6365 . SetCompatibilityVersion ( CompatibilityVersion . Version_3_0 ) ;
6466
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" ;
7979
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 ( ) ;
9186
9287 services . AddAutoMapper ( typeof ( FoodMappings ) ) ;
9388 }
0 commit comments