1919using Microsoft . Extensions . Hosting ;
2020using Microsoft . OpenApi . Models ;
2121using Newtonsoft . Json . Serialization ;
22+ using System . IO ;
2223
2324namespace AbpCompanyName . AbpProjectName . Web . Host . Startup
2425{
@@ -41,10 +42,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4142 {
4243 //MVC
4344 services . AddControllersWithViews (
44- options =>
45- {
46- options . Filters . Add ( new AbpAutoValidateAntiforgeryTokenAttribute ( ) ) ;
47- }
45+ options => { options . Filters . Add ( new AbpAutoValidateAntiforgeryTokenAttribute ( ) ) ; }
4846 ) . AddNewtonsoftJson ( options =>
4947 {
5048 options . SerializerSettings . ContractResolver = new AbpMvcContractResolver ( IocManager . Instance )
@@ -53,8 +51,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
5351 } ;
5452 } ) ;
5553
56-
57-
5854 IdentityRegistrar . Register ( services ) ;
5955 AuthConfigurer . Configure ( services , _appConfiguration ) ;
6056
@@ -79,51 +75,21 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7975 ) ;
8076
8177 // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
82- services . AddSwaggerGen ( options =>
83- {
84- options . SwaggerDoc ( _apiVersion , new OpenApiInfo
85- {
86- Version = _apiVersion ,
87- Title = "AbpProjectName API" ,
88- Description = "AbpProjectName" ,
89- // uncomment if needed TermsOfService = new Uri("https://example.com/terms"),
90- Contact = new OpenApiContact
91- {
92- Name = "AbpProjectName" ,
93- Email = string . Empty ,
94- Url = new Uri ( "https://twitter.com/aspboilerplate" ) ,
95- } ,
96- License = new OpenApiLicense
97- {
98- Name = "MIT License" ,
99- Url = new Uri ( "https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/LICENSE" ) ,
100- }
101- } ) ;
102- options . DocInclusionPredicate ( ( docName , description ) => true ) ;
103-
104- // Define the BearerAuth scheme that's in use
105- options . AddSecurityDefinition ( "bearerAuth" , new OpenApiSecurityScheme ( )
106- {
107- Description = "JWT Authorization header using the Bearer scheme. Example: \" Authorization: Bearer {token}\" " ,
108- Name = "Authorization" ,
109- In = ParameterLocation . Header ,
110- Type = SecuritySchemeType . ApiKey
111- } ) ;
112- } ) ;
78+ ConfigureSwagger ( services ) ;
11379
11480 // Configure Abp and Dependency Injection
11581 return services . AddAbp < AbpProjectNameWebHostModule > (
11682 // Configure Log4Net logging
11783 options => options . IocManager . IocContainer . AddFacility < LoggingFacility > (
11884 f => f . UseAbpLog4Net ( ) . WithConfig ( _hostingEnvironment . IsDevelopment ( )
119- ? "log4net.config"
120- : "log4net.Production.config"
121- )
85+ ? "log4net.config"
86+ : "log4net.Production.config"
87+ )
12288 )
12389 ) ;
12490 }
12591
126- public void Configure ( IApplicationBuilder app , ILoggerFactory loggerFactory )
92+ public void Configure ( IApplicationBuilder app , ILoggerFactory loggerFactory )
12793 {
12894 app . UseAbp ( options => { options . UseAbpRequestLocalization = false ; } ) ; // Initializes ABP framework.
12995
@@ -137,7 +103,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
137103
138104 app . UseAbpRequestLocalization ( ) ;
139105
140-
106+
141107 app . UseEndpoints ( endpoints =>
142108 {
143109 endpoints . MapHub < AbpCommonHub > ( "/signalr" ) ;
@@ -158,5 +124,58 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
158124 options . DisplayRequestDuration ( ) ; // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
159125 } ) ; // URL: /swagger
160126 }
127+
128+ private void ConfigureSwagger ( IServiceCollection services )
129+ {
130+ services . AddSwaggerGen ( options =>
131+ {
132+ options . SwaggerDoc ( _apiVersion , new OpenApiInfo
133+ {
134+ Version = _apiVersion ,
135+ Title = "AbpProjectName API" ,
136+ Description = "AbpProjectName" ,
137+ // uncomment if needed TermsOfService = new Uri("https://example.com/terms"),
138+ Contact = new OpenApiContact
139+ {
140+ Name = "AbpProjectName" ,
141+ Email = string . Empty ,
142+ Url = new Uri ( "https://twitter.com/aspboilerplate" ) ,
143+ } ,
144+ License = new OpenApiLicense
145+ {
146+ Name = "MIT License" ,
147+ Url = new Uri ( "https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/LICENSE" ) ,
148+ }
149+ } ) ;
150+ options . DocInclusionPredicate ( ( docName , description ) => true ) ;
151+
152+ // Define the BearerAuth scheme that's in use
153+ options . AddSecurityDefinition ( "bearerAuth" , new OpenApiSecurityScheme ( )
154+ {
155+ Description =
156+ "JWT Authorization header using the Bearer scheme. Example: \" Authorization: Bearer {token}\" " ,
157+ Name = "Authorization" ,
158+ In = ParameterLocation . Header ,
159+ Type = SecuritySchemeType . ApiKey
160+ } ) ;
161+
162+ //add summaries to swagger
163+ bool canShowSummaries = _appConfiguration . GetValue < bool > ( "Swagger:ShowSummaries" ) ;
164+ if ( canShowSummaries )
165+ {
166+ var hostXmlFile = $ "{ Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name } .xml";
167+ var hostXmlPath = Path . Combine ( AppContext . BaseDirectory , hostXmlFile ) ;
168+ options . IncludeXmlComments ( hostXmlPath ) ;
169+
170+ var applicationXml = $ "AbpCompanyName.AbpProjectName.Application.xml";
171+ var applicationXmlPath = Path . Combine ( AppContext . BaseDirectory , applicationXml ) ;
172+ options . IncludeXmlComments ( applicationXmlPath ) ;
173+
174+ var webCoreXmlFile = $ "AbpCompanyName.AbpProjectName.Web.Core.xml";
175+ var webCoreXmlPath = Path . Combine ( AppContext . BaseDirectory , webCoreXmlFile ) ;
176+ options . IncludeXmlComments ( webCoreXmlPath ) ;
177+ }
178+ } ) ;
179+ }
161180 }
162181}
0 commit comments