11using Microsoft . AspNetCore . Builder ;
22using Microsoft . AspNetCore . Hosting ;
3- using Microsoft . OpenApi . Models ;
4- using Microsoft . Extensions . Configuration ;
53using Microsoft . Extensions . DependencyInjection ;
64using Microsoft . Extensions . Hosting ;
7- using SampleApp9000 . Extensions ;
5+ using Microsoft . OpenApi ;
86using Swashbuckle . Rebase . Extensions ;
7+ using Swashbuckle . Servers . Extension . Extensions ;
98
10- namespace SampleApp9000
9+ namespace SampleApp9000 ;
10+
11+ public class Startup
1112{
12- public class Startup
13+ // This method gets called by the runtime. Use this method to add services to the container.
14+ public void ConfigureServices ( IServiceCollection services )
1315 {
14- public Startup ( IConfiguration configuration )
15- {
16- Configuration = configuration ;
17- }
18-
19- public IConfiguration Configuration { get ; }
16+ services . AddControllers ( ) ;
2017
21- // This method gets called by the runtime. Use this method to add services to the container.
22- public void ConfigureServices ( IServiceCollection services )
18+ services . AddSwaggerGen ( c =>
2319 {
24- services . AddControllers ( ) ;
25-
26- services . AddSwaggerGen ( c =>
27- {
28- c . SwaggerDoc
29- (
30- "sampleapp" ,
31- new OpenApiInfo
32- {
33- Title = "Sample App 9000" ,
34- Version = "1.0.0" ,
35- Description = "Provides a simple example of the tool"
36- }
37- ) ;
38- } ) ;
39- }
20+ c . SwaggerDoc
21+ (
22+ "sampleapp" ,
23+ new OpenApiInfo
24+ {
25+ Title = "Sample App 9000" ,
26+ Version = "1.0.0" ,
27+ Description = "Provides a simple example of the tool"
28+ }
29+ ) ;
30+ } ) ;
31+ }
4032
41- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
42- public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
33+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
34+ public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
35+ {
36+ if ( env . IsDevelopment ( ) )
4337 {
44- if ( env . IsDevelopment ( ) )
45- {
46- app . UseDeveloperExceptionPage ( ) ;
47- }
38+ app . UseDeveloperExceptionPage ( ) ;
39+ }
4840
49- app . UseHttpsRedirection ( ) ;
41+ app . UseHttpsRedirection ( ) ;
5042
51- app . UseRouting ( ) ;
43+ app . UseRouting ( ) ;
5244
53- app . UseAuthorization ( ) ;
54- app . UseSwagger
55- (
56- options =>
45+ app . UseAuthorization ( ) ;
46+ app . UseSwagger
47+ (
48+ options =>
49+ {
50+ options . PreSerializeFilters . Add ( ( swagger , httpReq ) =>
5751 {
58- options . PreSerializeFilters . Add ( ( swagger , httpReq ) =>
52+ if ( ! string . IsNullOrWhiteSpace ( httpReq ? . Host . Value ) )
5953 {
60- if ( ! string . IsNullOrWhiteSpace ( httpReq ? . Host . Value ) )
61- {
62- swagger . Servers . Add
63- (
64- new OpenApiServer { Url = $ "{ httpReq . Scheme } ://{ httpReq . Host . Value } {{basePath}}" }
65- . WithVariable ( "basePath" , new OpenApiServerVariable { Default = "/test" } )
66- ) ;
67- }
68- } ) ;
54+ swagger . Servers ??= [ ] ;
6955
70- options . RemoveRoot ( "/test" ) ;
71- }
72- ) ;
56+ swagger . Servers . Add
57+ (
58+ new OpenApiServer { Url = $ "{ httpReq . Scheme } ://{ httpReq . Host . Value } {{basePath}}" }
59+ . WithVariable ( "basePath" , new OpenApiServerVariable { Default = "/test" } )
60+ ) ;
61+ }
62+ } ) ;
7363
74- // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
75- // specifying the Swagger JSON endpoint.
76- app . UseSwaggerUI ( x =>
77- {
78- x . DocumentTitle = "SampleApp9000" ;
79- x . SwaggerEndpoint ( "/swagger/sampleapp/swagger.json" , "Sample App 9000" ) ;
80- x . RoutePrefix = string . Empty ;
81- } ) ;
64+ options . RemoveRoot ( "/test" ) ;
65+ }
66+ ) ;
8267
83- app . UseEndpoints ( endpoints =>
84- {
85- endpoints . MapControllers ( ) ;
86- } ) ;
87- }
68+ // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
69+ // specifying the Swagger JSON endpoint.
70+ app . UseSwaggerUI ( x =>
71+ {
72+ x . DocumentTitle = "SampleApp9000" ;
73+ x . SwaggerEndpoint ( "/swagger/sampleapp/swagger.json" , "Sample App 9000" ) ;
74+ x . RoutePrefix = string . Empty ;
75+ } ) ;
76+
77+ app . UseEndpoints ( endpoints =>
78+ {
79+ endpoints . MapControllers ( ) ;
80+ } ) ;
8881 }
89- }
82+ }
0 commit comments