Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 2c1680e

Browse files
committed
Aggiunta documentazione Swagger minima
1 parent ea50616 commit 2c1680e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/NET6CustomLibrary/Docs/README-Swagger.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
# SwaggerUI configuration
22

33

4-
## Registering services at Startup
4+
## Registering services at Startup (with xml documentation)
55

66
```csharp
77
public Startup(IConfiguration configuration)
88
{
99
Configuration = configuration;
1010
}
1111

12+
public IConfiguration Configuration { get; }
13+
14+
public void ConfigureServices(IServiceCollection services)
15+
{
16+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
17+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
18+
19+
services.AddSwaggerGenConfig("My Web Api", "v1", "Add here a description which will be shown in the swagger UI", true, xmlPath);
20+
//OR services.AddSwaggerGenConfig("My Web Api", "v1", string.Empty, true, xmlPath);
21+
}
22+
23+
//OMISSIS
24+
25+
public void Configure(WebApplication app)
26+
{
27+
app.AddUseSwaggerUI("My Web Api v1");
28+
}
29+
```
30+
31+
<b>Note:</b> If you want to add xml documentation to web api you need to add TRUE value to GenerateDocumentationFile tag to csproj project file.
32+
33+
34+
## Registering services at Startup (without xml documentation)
35+
36+
```csharp
37+
public Startup(IConfiguration configuration)
38+
{
39+
Configuration = configuration;
40+
}
41+
42+
public IConfiguration Configuration { get; }
43+
44+
public void ConfigureServices(IServiceCollection services)
45+
{
46+
services.AddSwaggerGenConfig("My Web Api", "v1", "Add here a description which will be shown in the swagger UI");
47+
//OR services.AddSwaggerGenConfig("My Web Api", "v1", string.Empty);
48+
}
49+
1250
//OMISSIS
1351
1452
public void Configure(WebApplication app)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace NET6CustomLibrary.Swagger;
2+
3+
public static class SwaggerSimple
4+
{
5+
public static IServiceCollection AddSwaggerGenConfig(this IServiceCollection services, string title,
6+
string version, string description = "", bool extendSchema = false, string xmlCommentsPath = "")
7+
{
8+
services
9+
.AddEndpointsApiExplorer()
10+
.AddSwaggerGen(options =>
11+
{
12+
options.OperationFilter<CultureAwareOperationFilter>();
13+
options.SwaggerDoc($"{version}", new OpenApiInfo
14+
{
15+
Title = $"{title}",
16+
Version = $"{version}",
17+
Description = $"{description}",
18+
});
19+
20+
if (extendSchema)
21+
options.UseAllOfToExtendReferenceSchemas();
22+
23+
if (xmlCommentsPath is not (null or ""))
24+
options.IncludeXmlComments(xmlCommentsPath);
25+
});
26+
27+
return services;
28+
}
29+
}

0 commit comments

Comments
 (0)