1+ namespace Microsoft . Examples
2+ {
3+ using Microsoft . AspNetCore . Builder ;
4+ using Microsoft . AspNetCore . Hosting ;
5+ using Microsoft . AspNetCore . Mvc . ApiExplorer ;
6+ using Microsoft . Extensions . Configuration ;
7+ using Microsoft . Extensions . DependencyInjection ;
8+ using Microsoft . Extensions . Logging ;
9+ using Microsoft . Extensions . PlatformAbstractions ;
10+ using Swashbuckle . AspNetCore . Swagger ;
11+ using System . IO ;
12+ using System . Reflection ;
13+
14+ /// <summary>
15+ /// Represents the startup process for the application.
16+ /// </summary>
17+ public class Startup
18+ {
19+ /// <summary>
20+ /// Initializes a new instance of the <see cref="Startup"/> class.
21+ /// </summary>
22+ /// <param name="env">The current hosting environment.</param>
23+ public Startup ( IHostingEnvironment env )
24+ {
25+ var builder = new ConfigurationBuilder ( )
26+ . SetBasePath ( env . ContentRootPath )
27+ . AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true )
28+ . AddJsonFile ( $ "appsettings.{ env . EnvironmentName } .json", optional : true )
29+ . AddEnvironmentVariables ( ) ;
30+
31+ Configuration = builder . Build ( ) ;
32+ }
33+
34+ /// <summary>
35+ /// Gets the current configuration.
36+ /// </summary>
37+ /// <value>The current application configuration.</value>
38+ public IConfigurationRoot Configuration { get ; }
39+
40+ /// <summary>
41+ /// Configures services for the application.
42+ /// </summary>
43+ /// <param name="services">The collection of services to configure the application with.</param>
44+ public void ConfigureServices ( IServiceCollection services )
45+ {
46+ // add the versioned api explorer, which also adds the following services:
47+ //
48+ // * IApiVersionDescriptionProvider
49+ // * IApiVersionGroupNameFormatter
50+ services . AddMvcCore ( ) . AddVersionedApiExplorer ( ) ;
51+
52+ services . AddMvc ( ) ;
53+ services . AddApiVersioning ( o => o . ReportApiVersions = true ) ;
54+ services . AddSwaggerGen (
55+ options =>
56+ {
57+ // resolve the IApiVersionDescriptionProvider service
58+ // note: that we have to build a temporary service provider here because one has not been created yet
59+ var provider = services . BuildServiceProvider ( ) . GetRequiredService < IApiVersionDescriptionProvider > ( ) ;
60+
61+ // add a swagger document for each discovered API version
62+ // note: you might choose to skip or document deprecated API versions differently
63+ foreach ( var description in provider . ApiVersionDescriptions )
64+ {
65+ options . SwaggerDoc ( description . GroupName , CreateInfoForApiVersion ( description ) ) ;
66+ }
67+
68+ // add a custom operation filter which documents the implicit API version parameter
69+ options . OperationFilter < ImplicitApiVersionParameter > ( ) ;
70+
71+ // integrate xml comments
72+ options . IncludeXmlComments ( XmlCommentsFilePath ) ;
73+ } ) ;
74+ }
75+
76+ /// <summary>
77+ /// Configures the application using the provided builder, hosting environment, and logging factory.
78+ /// </summary>
79+ /// <param name="app">The current application builder.</param>
80+ /// <param name="env">The current hosting environment.</param>
81+ /// <param name="loggerFactory">The logging factory used for instrumentation.</param>
82+ public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory )
83+ {
84+ loggerFactory . AddConsole ( Configuration . GetSection ( "Logging" ) ) ;
85+ loggerFactory . AddDebug ( ) ;
86+
87+ app . UseMvc ( ) ;
88+ app . UseSwagger ( ) ;
89+ app . UseSwaggerUI (
90+ options =>
91+ {
92+ // resolve the IApiVersionDescriptionProvider service
93+ var provider = app . ApplicationServices . GetRequiredService < IApiVersionDescriptionProvider > ( ) ;
94+
95+ // build a swagger endpoint for each discovered API version
96+ foreach ( var description in provider . ApiVersionDescriptions )
97+ {
98+ options . SwaggerEndpoint ( $ "/swagger/{ description . GroupName } /swagger.json", description . GroupName . ToUpperInvariant ( ) ) ;
99+ }
100+ } ) ;
101+ }
102+
103+ static string XmlCommentsFilePath
104+ {
105+ get
106+ {
107+ var basePath = PlatformServices . Default . Application . ApplicationBasePath ;
108+ var fileName = typeof ( Startup ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Name + ".xml" ;
109+ return Path . Combine ( basePath , fileName ) ;
110+ }
111+ }
112+
113+ static Info CreateInfoForApiVersion ( ApiVersionDescription description )
114+ {
115+ var info = new Info ( )
116+ {
117+ Title = $ "Sample API { description . ApiVersion } ",
118+ Version = description . ApiVersion . ToString ( ) ,
119+ Description = "A sample application with Swagger, Swashbuckle, and API versioning." ,
120+ Contact = new Contact ( ) { Name = "Bill Mei" , Email = "bill.mei@somewhere.com" } ,
121+ TermsOfService = "Shareware" ,
122+ License = new License ( ) { Name = "MIT" , Url = "https://opensource.org/licenses/MIT" }
123+ } ;
124+
125+ if ( description . IsDeprecated )
126+ {
127+ info . Description += " This API version has been deprecated." ;
128+ }
129+
130+ return info ;
131+ }
132+ }
133+ }
0 commit comments