Skip to content

Commit d87c506

Browse files
spboyerscottaddie
andauthored
Swashbuckle update (#18974)
* Update to current version * cleanup AddSwaggerGen snippets * moving OpenApiInfo to proper location * powerapps and flow version note 2.0 example * annotations update * annotations edit * edit pass * fix note * update line number Co-authored-by: Scott Addie <[email protected]>
1 parent d26d4aa commit d87c506

File tree

5 files changed

+81
-40
lines changed

5 files changed

+81
-40
lines changed

aspnetcore/tutorials/getting-started-with-swashbuckle.md

+31-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: zuckerthoben
44
description: Learn how to add Swashbuckle to your ASP.NET Core web API project to integrate the Swagger UI.
55
ms.author: scaddie
66
ms.custom: mvc
7-
ms.date: 01/17/2020
7+
ms.date: 06/26/2020
88
no-loc: [Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
99
uid: tutorials/get-started-with-swashbuckle
1010
---
@@ -34,7 +34,7 @@ Swashbuckle can be added with the following approaches:
3434
* Execute the following command:
3535

3636
```powershell
37-
Install-Package Swashbuckle.AspNetCore -Version 5.0.0
37+
Install-Package Swashbuckle.AspNetCore -Version 5.5.0
3838
```
3939
4040
* From the **Manage NuGet Packages** dialog:
@@ -57,42 +57,38 @@ Swashbuckle can be added with the following approaches:
5757
Run the following command from the **Integrated Terminal**:
5858
5959
```dotnetcli
60-
dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 5.0.0
60+
dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 5.5.0
6161
```
6262

6363
### [.NET Core CLI](#tab/netcore-cli)
6464

6565
Run the following command:
6666

6767
```dotnetcli
68-
dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 5.0.0
68+
dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 5.5.0
6969
```
7070

7171
---
7272

7373
## Add and configure Swagger middleware
7474

75-
In the `Startup` class, import the following namespace to use the `OpenApiInfo` class:
76-
77-
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/Startup2.cs?name=snippet_InfoClassNamespace)]
78-
7975
Add the Swagger generator to the services collection in the `Startup.ConfigureServices` method:
8076

8177
::: moniker range="<= aspnetcore-2.0"
8278

83-
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/Startup2.cs?name=snippet_ConfigureServices&highlight=8-11)]
79+
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/Startup2.cs?name=snippet_ConfigureServices&highlight=8)]
8480

8581
::: moniker-end
8682

8783
::: moniker range=">= aspnetcore-2.1 <= aspnetcore-2.2"
8884

89-
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.1/TodoApi.Swashbuckle/Startup2.cs?name=snippet_ConfigureServices&highlight=9-12)]
85+
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.1/TodoApi.Swashbuckle/Startup2.cs?name=snippet_ConfigureServices&highlight=9)]
9086

9187
::: moniker-end
9288

9389
::: moniker range=">= aspnetcore-3.0"
9490

95-
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/3.0/TodoApi.Swashbuckle/Startup2.cs?name=snippet_ConfigureServices&highlight=8-11)]
91+
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/3.0/TodoApi.Swashbuckle/Startup2.cs?name=snippet_ConfigureServices&highlight=8)]
9692

9793
::: moniker-end
9894

@@ -110,6 +106,9 @@ In the `Startup.Configure` method, enable the middleware for serving the generat
110106

111107
::: moniker-end
112108

109+
> [!NOTE]
110+
> Swashbuckle relies on MVC's <xref:Microsoft.AspNetCore.Mvc.ApiExplorer> to discover the routes and endpoints. If the project calls <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc%2A>, routes and endpoints are discovered automatically. When calling <xref:Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore%2A>, the <xref:Microsoft.Extensions.DependencyInjection.MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer%2A> method must be explicitly called. For more information, see [Swashbuckle, ApiExplorer, and Routing](https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckle-apiexplorer-and-routing).
111+
113112
The preceding `UseSwaggerUI` method call enables the [Static File Middleware](xref:fundamentals/static-files). If targeting .NET Framework or .NET Core 1.x, add the [Microsoft.AspNetCore.StaticFiles](https://www.nuget.org/packages/Microsoft.AspNetCore.StaticFiles/) NuGet package to the project.
114113

115114
Launch the app, and navigate to `http://localhost:<port>/swagger/v1/swagger.json`. The generated document describing the endpoints appears as shown in [Swagger specification (swagger.json)](xref:tutorials/web-api-help-pages-using-swagger#swagger-specification-swaggerjson).
@@ -123,6 +122,11 @@ The Swagger UI can be found at `http://localhost:<port>/swagger`. Explore the AP
123122
124123
If using directories with IIS or a reverse proxy, set the Swagger endpoint to a relative path using the `./` prefix. For example, `./swagger/v1/swagger.json`. Using `/swagger/v1/swagger.json` instructs the app to look for the JSON file at the true root of the URL (plus the route prefix, if used). For example, use `http://localhost:<port>/<route_prefix>/swagger/v1/swagger.json` instead of `http://localhost:<port>/<virtual_directory>/<route_prefix>/swagger/v1/swagger.json`.
125124

125+
> [!NOTE]
126+
> By default, Swashbuckle generates and exposes Swagger JSON in version 3.0 of the specification&mdash;officially called the OpenAPI Specification. To support backwards compatibility, you can opt into exposing JSON in the 2.0 format instead. This 2.0 format is important for integrations such as Microsoft Power Apps and Microsoft Flow that currently support OpenAPI version 2.0. To opt into the 2.0 format, set the `SerializeAsV2` property in `Startup.Configure`:
127+
>
128+
> [!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/3.0/TodoApi.Swashbuckle/Startup3.cs?name=snippet_Configure&highlight=4-7)]
129+
126130
## Customize and extend
127131

128132
Swagger provides options for documenting the object model and customizing the UI to match your theme.
@@ -139,6 +143,12 @@ using System.IO;
139143

140144
The configuration action passed to the `AddSwaggerGen` method adds information such as the author, license, and description:
141145

146+
In the `Startup` class, import the following namespace to use the `OpenApiInfo` class:
147+
148+
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/Startup2.cs?name=snippet_InfoClassNamespace)]
149+
150+
Using the `OpenApiInfo` class, modify the information displayed in the UI:
151+
142152
[!code-csharp[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/Startup4.cs?name=snippet_AddSwaggerGen)]
143153

144154
The Swagger UI displays the version's information:
@@ -440,11 +450,13 @@ The Swagger UI now clearly documents the expected HTTP response codes:
440450

441451
In ASP.NET Core 2.2 or later, conventions can be used as an alternative to explicitly decorating individual actions with `[ProducesResponseType]`. For more information, see <xref:web-api/advanced/conventions>.
442452

453+
To support the `[ProducesResponseType]` decoration, the [Swashbuckle.AspNetCore.Annotations](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#swashbuckleaspnetcoreannotations) package offers extensions to enable and enrich the response, schema, and parameter metadata.
454+
443455
::: moniker-end
444456

445457
### Customize the UI
446458

447-
The stock UI is both functional and presentable. However, API documentation pages should represent your brand or theme. Branding the Swashbuckle components requires adding the resources to serve static files and building the folder structure to host those files.
459+
The default UI is both functional and presentable. However, API documentation pages should represent your brand or theme. Branding the Swashbuckle components requires adding the resources to serve static files and building the folder structure to host those files.
448460

449461
If targeting .NET Framework or .NET Core 1.x, add the [Microsoft.AspNetCore.StaticFiles](https://www.nuget.org/packages/Microsoft.AspNetCore.StaticFiles) NuGet package to the project:
450462

@@ -468,20 +480,11 @@ Enable Static File Middleware:
468480

469481
::: moniker-end
470482

471-
Acquire the contents of the *dist* folder from the [Swagger UI GitHub repository](https://github.com/swagger-api/swagger-ui/tree/master/dist). This folder contains the necessary assets for the Swagger UI page.
472-
473-
Create a *wwwroot/swagger/ui* folder, and copy into it the contents of the *dist* folder.
474-
475-
Create a *custom.css* file, in *wwwroot/swagger/ui*, with the following CSS to customize the page header:
476-
477-
[!code-css[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/wwwroot/swagger/ui/custom.css)]
478-
479-
Reference *custom.css* in the *index.html* file inside ui folder, after any other CSS files:
480-
481-
[!code-html[](../tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/wwwroot/swagger/ui/index.html?name=snippet_SwaggerUiCss&highlight=3)]
483+
To inject additional CSS stylesheets, add them to the project's *wwwroot* folder and specify the relative path in the middleware options:
482484

483-
Browse to the *index.html* page at `http://localhost:<port>/swagger/ui/index.html`. Enter `https://localhost:<port>/swagger/v1/swagger.json` in the header's textbox, and click the **Explore** button. The resulting page looks as follows:
484-
485-
![Swagger UI with custom header title](web-api-help-pages-using-swagger/_static/custom-header.png)
486-
487-
There's much more you can do with the page. See the full capabilities for the UI resources at the [Swagger UI GitHub repository](https://github.com/swagger-api/swagger-ui).
485+
```csharp
486+
app.UseSwaggerUI(c =>
487+
{
488+
c.InjectStylesheet("/swagger-ui/custom.css");
489+
}
490+
```

aspnetcore/tutorials/web-api-help-pages-using-swagger/samples/2.0/TodoApi.Swashbuckle/Startup2.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public void ConfigureServices(IServiceCollection services)
2222
services.AddMvc();
2323

2424
// Register the Swagger generator, defining 1 or more Swagger documents
25-
services.AddSwaggerGen(c =>
26-
{
27-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
28-
});
25+
services.AddSwaggerGen();
2926
}
3027
#endregion
3128

aspnetcore/tutorials/web-api-help-pages-using-swagger/samples/2.1/TodoApi.Swashbuckle/Startup2.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ public void ConfigureServices(IServiceCollection services)
1818
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
1919

2020
// Register the Swagger generator, defining 1 or more Swagger documents
21-
services.AddSwaggerGen(c =>
22-
{
23-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
24-
});
21+
services.AddSwaggerGen();
2522
}
2623
#endregion
2724

aspnetcore/tutorials/web-api-help-pages-using-swagger/samples/3.0/TodoApi.Swashbuckle/Startup2.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ public void ConfigureServices(IServiceCollection services)
1616
services.AddControllers();
1717

1818
// Register the Swagger generator, defining 1 or more Swagger documents
19-
services.AddSwaggerGen(c =>
20-
{
21-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
22-
});
19+
services.AddSwaggerGen();
2320
}
2421
#endregion
2522

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.OpenApi.Models;
5+
using TodoApi.Models;
6+
7+
namespace TodoApi
8+
{
9+
public class Startup2
10+
{
11+
#region snippet_ConfigureServices
12+
public void ConfigureServices(IServiceCollection services)
13+
{
14+
services.AddDbContext<TodoContext>(opt =>
15+
opt.UseInMemoryDatabase("TodoList"));
16+
services.AddControllers();
17+
18+
// Register the Swagger generator, defining 1 or more Swagger documents
19+
services.AddSwaggerGen();
20+
}
21+
#endregion
22+
23+
#region snippet_Configure
24+
public void Configure(IApplicationBuilder app)
25+
{
26+
// Enable middleware to serve generated Swagger as a JSON endpoint.
27+
app.UseSwagger(c =>
28+
{
29+
c.SerializeAsV2 = true;
30+
});
31+
32+
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
33+
// specifying the Swagger JSON endpoint.
34+
app.UseSwaggerUI(c =>
35+
{
36+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
37+
});
38+
39+
app.UseRouting();
40+
app.UseEndpoints(endpoints =>
41+
{
42+
endpoints.MapControllers();
43+
});
44+
}
45+
#endregion
46+
}
47+
}

0 commit comments

Comments
 (0)