Skip to content

Commit 4a6f3e3

Browse files
committed
moved swagger configuration to a separate method
1 parent 807bde5 commit 4a6f3e3

File tree

1 file changed

+55
-52
lines changed
  • aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup

1 file changed

+55
-52
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/Startup.cs

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4242
{
4343
//MVC
4444
services.AddControllersWithViews(
45-
options =>
46-
{
47-
options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
48-
}
45+
options => { options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute()); }
4946
).AddNewtonsoftJson(options =>
5047
{
5148
options.SerializerSettings.ContractResolver = new AbpMvcContractResolver(IocManager.Instance)
@@ -78,6 +75,58 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7875
);
7976

8077
// Swagger - Enable this line and the related lines in Configure method to enable swagger UI
78+
ConfigureSwagger(services);
79+
80+
// Configure Abp and Dependency Injection
81+
return services.AddAbp<AbpProjectNameWebHostModule>(
82+
// Configure Log4Net logging
83+
options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
84+
f => f.UseAbpLog4Net().WithConfig(_hostingEnvironment.IsDevelopment()
85+
? "log4net.config"
86+
: "log4net.Production.config"
87+
)
88+
)
89+
);
90+
}
91+
92+
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
93+
{
94+
app.UseAbp(options => { options.UseAbpRequestLocalization = false; }); // Initializes ABP framework.
95+
96+
app.UseCors(_defaultCorsPolicyName); // Enable CORS!
97+
98+
app.UseStaticFiles();
99+
100+
app.UseRouting();
101+
102+
app.UseAuthentication();
103+
104+
app.UseAbpRequestLocalization();
105+
106+
107+
app.UseEndpoints(endpoints =>
108+
{
109+
endpoints.MapHub<AbpCommonHub>("/signalr");
110+
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
111+
endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
112+
});
113+
114+
// Enable middleware to serve generated Swagger as a JSON endpoint
115+
app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
116+
117+
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
118+
app.UseSwaggerUI(options =>
119+
{
120+
// specifying the Swagger JSON endpoint.
121+
options.SwaggerEndpoint($"/swagger/{_apiVersion}/swagger.json", $"AbpProjectName API {_apiVersion}");
122+
options.IndexStream = () => Assembly.GetExecutingAssembly()
123+
.GetManifestResourceStream("AbpCompanyName.AbpProjectName.Web.Host.wwwroot.swagger.ui.index.html");
124+
options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
125+
}); // URL: /swagger
126+
}
127+
128+
private void ConfigureSwagger(IServiceCollection services)
129+
{
81130
services.AddSwaggerGen(options =>
82131
{
83132
options.SwaggerDoc(_apiVersion, new OpenApiInfo
@@ -103,7 +152,8 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
103152
// Define the BearerAuth scheme that's in use
104153
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
105154
{
106-
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
155+
Description =
156+
"JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
107157
Name = "Authorization",
108158
In = ParameterLocation.Header,
109159
Type = SecuritySchemeType.ApiKey
@@ -126,53 +176,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
126176
options.IncludeXmlComments(webCoreXmlPath);
127177
}
128178
});
129-
130-
// Configure Abp and Dependency Injection
131-
return services.AddAbp<AbpProjectNameWebHostModule>(
132-
// Configure Log4Net logging
133-
options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
134-
f => f.UseAbpLog4Net().WithConfig(_hostingEnvironment.IsDevelopment()
135-
? "log4net.config"
136-
: "log4net.Production.config"
137-
)
138-
)
139-
);
140-
}
141-
142-
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
143-
{
144-
app.UseAbp(options => { options.UseAbpRequestLocalization = false; }); // Initializes ABP framework.
145-
146-
app.UseCors(_defaultCorsPolicyName); // Enable CORS!
147-
148-
app.UseStaticFiles();
149-
150-
app.UseRouting();
151-
152-
app.UseAuthentication();
153-
154-
app.UseAbpRequestLocalization();
155-
156-
157-
app.UseEndpoints(endpoints =>
158-
{
159-
endpoints.MapHub<AbpCommonHub>("/signalr");
160-
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
161-
endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
162-
});
163-
164-
// Enable middleware to serve generated Swagger as a JSON endpoint
165-
app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
166-
167-
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
168-
app.UseSwaggerUI(options =>
169-
{
170-
// specifying the Swagger JSON endpoint.
171-
options.SwaggerEndpoint($"/swagger/{_apiVersion}/swagger.json", $"AbpProjectName API {_apiVersion}");
172-
options.IndexStream = () => Assembly.GetExecutingAssembly()
173-
.GetManifestResourceStream("AbpCompanyName.AbpProjectName.Web.Host.wwwroot.swagger.ui.index.html");
174-
options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
175-
}); // URL: /swagger
176179
}
177180
}
178181
}

0 commit comments

Comments
 (0)