Skip to content

Commit 217c7ec

Browse files
authored
Remove Swagger & NSwag docs from TOC when .NET 9 version is selected (#33431)
1 parent 419d068 commit 217c7ec

File tree

3 files changed

+3
-258
lines changed

3 files changed

+3
-258
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ author: zuckerthoben
44
description: Learn how to use NSwag to generate documentation and help pages for an ASP.NET Core web API.
55
ms.author: wpickett
66
ms.custom: mvc
7+
monikerRange: ">= aspnetcore-3.1 <= aspnetcore-8.0"
78
ms.date: 12/05/2019
89
uid: tutorials/get-started-with-nswag
910
---

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

Lines changed: 1 addition & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -3,266 +3,11 @@ title: Get started with Swashbuckle and ASP.NET Core
33
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: wpickett
6-
monikerRange: '>= aspnetcore-3.1'
6+
monikerRange: ">= aspnetcore-3.1 <= aspnetcore-8.0"
77
ms.custom: mvc
88
ms.date: 05/14/2024
99
uid: tutorials/get-started-with-swashbuckle
1010
---
1111
# Get started with Swashbuckle and ASP.NET Core
1212

13-
:::moniker range=">= aspnetcore-9.0"
14-
15-
There are three main components to Swashbuckle:
16-
17-
* [Swashbuckle.AspNetCore.Swagger](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Swagger/): a Swagger object model and middleware to expose `SwaggerDocument` objects as JSON endpoints.
18-
19-
* [Swashbuckle.AspNetCore.SwaggerGen](https://www.nuget.org/packages/Swashbuckle.AspNetCore.SwaggerGen/): a Swagger generator that builds `SwaggerDocument` objects directly from your routes, controllers, and models. It's typically combined with the Swagger endpoint middleware to automatically expose Swagger JSON.
20-
21-
* [Swashbuckle.AspNetCore.SwaggerUI](https://www.nuget.org/packages/Swashbuckle.AspNetCore.SwaggerUI/): an embedded version of the Swagger UI tool. It interprets Swagger JSON to build a rich, customizable experience for describing the web API functionality. It includes built-in test harnesses for the public methods.
22-
23-
## Package installation
24-
25-
Swashbuckle can be added with the following approaches:
26-
27-
### [Visual Studio](#tab/visual-studio)
28-
29-
* From the **Package Manager Console** window:
30-
* Go to **View** > **Other Windows** > **Package Manager Console**
31-
* Navigate to the directory in which the `.csproj` file exists
32-
* Execute the following command:
33-
34-
```powershell
35-
Install-Package Swashbuckle.AspNetCore -Version 6.6.2
36-
```
37-
38-
* From the **Manage NuGet Packages** dialog:
39-
* Right-click the project in **Solution Explorer** > **Manage NuGet Packages**
40-
* Set the **Package source** to "nuget.org"
41-
* Ensure the "Include prerelease" option is enabled
42-
* Enter "Swashbuckle.AspNetCore" in the search box
43-
* Select the latest "Swashbuckle.AspNetCore" package from the **Browse** tab and click **Install**
44-
45-
### [Visual Studio for Mac](#tab/visual-studio-mac)
46-
47-
* Right-click the *Packages* folder in **Solution Pad** > **Add Packages...**
48-
* Set the **Add Packages** window's **Source** drop-down to "nuget.org"
49-
* Ensure the "Show pre-release packages" option is enabled
50-
* Enter "Swashbuckle.AspNetCore" in the search box
51-
* Select the latest "Swashbuckle.AspNetCore" package from the results pane and click **Add Package**
52-
53-
### [Visual Studio Code](#tab/visual-studio-code)
54-
55-
Run the following command from the **Integrated Terminal**:
56-
57-
```dotnetcli
58-
dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 6.6.2
59-
```
60-
61-
### [.NET CLI](#tab/net-cli)
62-
63-
Run the following command:
64-
65-
```dotnetcli
66-
dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 6.6.2
67-
```
68-
69-
---
70-
71-
## Add and configure Swagger middleware
72-
73-
Add the Swagger generator to the services collection in `Program.cs`:
74-
75-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Snippets/Program.cs" id="snippet_ServicesDefault" highlight="4":::
76-
77-
The call to <xref:Microsoft.Extensions.DependencyInjection.EndpointMetadataApiExplorerServiceCollectionExtensions.AddEndpointsApiExplorer%2A> shown in the preceding example is required only for [minimal APIs](/aspnet/core/fundamentals/minimal-apis/overview). For more information, see [this StackOverflow post](https://stackoverflow.com/a/71933535).
78-
79-
Enable the middleware for serving the generated JSON document and the Swagger UI, also in `Program.cs`:
80-
81-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Program.cs" id="snippet_Middleware" highlight="3,4":::
82-
83-
The preceding code adds the Swagger middleware only if the current environment is set to Development. The `UseSwaggerUI` method call enables an embedded version of the Swagger UI tool.
84-
85-
Launch the app and navigate to `https://localhost:<port>/swagger/v1/swagger.json`. The generated document describing the endpoints appears as shown in [OpenAPI specification (openapi.json)](xref:tutorials/web-api-help-pages-using-swagger#openapi-specification-openapijson).
86-
87-
The Swagger UI can be found at `https://localhost:<port>/swagger`. Explore the API via Swagger UI and incorporate it in other programs.
88-
89-
> [!TIP]
90-
> To serve the Swagger UI at the app's root (`https://localhost:<port>/`), set the `RoutePrefix` property to an empty string:
91-
>
92-
> :::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Snippets/Program.cs" id="snippet_MiddlewareRoutePrefix" highlight="6":::
93-
94-
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 `https://localhost:<port>/<route_prefix>/swagger/v1/swagger.json` instead of `https://localhost:<port>/<virtual_directory>/<route_prefix>/swagger/v1/swagger.json`.
95-
96-
> [!NOTE]
97-
> 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 `Program.cs`:
98-
>
99-
> :::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Snippets/Program.cs" id="snippet_MiddlewareJsonV2" highlight="3":::
100-
101-
## Customize and extend
102-
103-
Swagger provides options for documenting the object model and customizing the UI to match your theme.
104-
105-
### API info and description
106-
107-
The configuration action passed to the `AddSwaggerGen` method adds information such as the author, license, and description.
108-
109-
In `Program.cs`, import the following namespace to use the `OpenApiInfo` class:
110-
H
111-
source="~/tutorials/web-api-help-pages-using-swagger
112-
113-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Program.cs" id="snippet_UsingOpenApiModels":::
114-
115-
Using the `OpenApiInfo` class, modify the information displayed in the UI:
116-
117-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Snippets/Program.cs" id="snippet_ServicesOpenApiInfo" highlight="3-19":::
118-
119-
The Swagger UI displays the version's information:
120-
121-
:::image source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-info.png" alt-text="Swagger UI with version information: description, author, and license.":::
122-
123-
### XML comments
124-
125-
XML comments can be enabled with the following approaches:
126-
127-
#### [Visual Studio](#tab/visual-studio)
128-
129-
* Right-click the project in **Solution Explorer** and select *`Edit <project_name>.csproj`*.
130-
* Add [GenerateDocumentationFile](/dotnet/core/project-sdk/msbuild-props#generatedocumentationfile) to the `.csproj` file:
131-
132-
```XML
133-
<PropertyGroup>
134-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
135-
</PropertyGroup>
136-
```
137-
138-
#### [Visual Studio for Mac](#tab/visual-studio-mac)
139-
140-
* From the *Solution Pad*, press **control** and click the project name. Navigate to **Tools** > **Edit File**.
141-
* Add [GenerateDocumentationFile](/dotnet/core/project-sdk/msbuild-props#generatedocumentationfile) to the `.csproj` file:
142-
143-
```XML
144-
<PropertyGroup>
145-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
146-
</PropertyGroup>
147-
```
148-
149-
#### [Visual Studio Code](#tab/visual-studio-code)
150-
151-
Add [GenerateDocumentationFile](/dotnet/core/project-sdk/msbuild-props#generatedocumentationfile) to the `.csproj` file:
152-
153-
```XML
154-
<PropertyGroup>
155-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
156-
</PropertyGroup>
157-
```
158-
159-
#### [.NET CLI](#tab/net-cli)
160-
161-
Add [GenerateDocumentationFile](/dotnet/core/project-sdk/msbuild-props#generatedocumentationfile) to the `.csproj` file:
162-
163-
```XML
164-
<PropertyGroup>
165-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
166-
</PropertyGroup>
167-
```
168-
169-
---
170-
171-
Enabling XML comments provides debug information for undocumented public types and members. Undocumented types and members are indicated by the warning message. For example, the following message indicates a violation of warning code 1591:
172-
173-
```text
174-
warning CS1591: Missing XML comment for publicly visible type or member 'TodoController'
175-
```
176-
177-
To suppress warnings project-wide, define a semicolon-delimited list of warning codes to ignore in the project file. Appending the warning codes to `$(NoWarn);` applies the [C# default values](https://github.com/dotnet/sdk/blob/2eb6c546931b5bcb92cd3128b93932a980553ea1/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.props#L16) too.
178-
179-
:::code language="xml" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/SwashbuckleSample.csproj" range="9-12" highlight="3":::
180-
181-
To suppress warnings only for specific members, enclose the code in [#pragma warning](/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma-warning) preprocessor directives. This approach is useful for code that shouldn't be exposed via the API docs. In the following example, warning code CS1591 is ignored for the entire `TodoContext` class. Enforcement of the warning code is restored at the close of the class definition. Specify multiple warning codes with a comma-delimited list.
182-
183-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Models/TodoContext.cs" id="snippet_PragmaWarningDisable" highlight="3,10":::
184-
185-
Configure Swagger to use the XML file that's generated with the preceding instructions. For Linux or non-Windows operating systems, file names and paths can be case-sensitive. For example, a `TodoApi.XML` file is valid on Windows but not Ubuntu.
186-
187-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Program.cs" id="snippet_Services" highlight="22-23":::
188-
189-
In the preceding code, [Reflection](/dotnet/csharp/programming-guide/concepts/reflection) is used to build an XML file name matching that of the web API project. The [AppContext.BaseDirectory](xref:System.AppContext.BaseDirectory%2A) property is used to construct a path to the XML file. Some Swagger features (for example, schemata of input parameters or HTTP methods and response codes from the respective attributes) work without the use of an XML documentation file. For most features, namely method summaries and the descriptions of parameters and response codes, the use of an XML file is mandatory.
190-
191-
Adding triple-slash comments to an action enhances the Swagger UI by adding the description to the section header. Add a [\<summary>](/dotnet/csharp/programming-guide/xmldoc/summary) element above the `Delete` action:
192-
193-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Controllers/TodoController.cs" id="snippet_Delete" highlight="1-3":::
194-
195-
The Swagger UI displays the inner text of the preceding code's `<summary>` element:
196-
197-
:::image source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-delete-summary.png" alt-text="Swagger UI showing XML comment 'Deletes a specific TodoItem.' for the DELETE method.":::
198-
199-
The UI is driven by the generated JSON schema:
200-
201-
:::code language="json" source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-delete.json" range="2-24":::
202-
203-
Add a [\<remarks>](/dotnet/csharp/programming-guide/xmldoc/remarks) element to the `Create` action method documentation. It supplements information specified in the `<summary>` element and provides a more robust Swagger UI. The `<remarks>` element content can consist of text, JSON, or XML.
204-
205-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Controllers/TodoController.cs" id="snippet_Create" highlight="6-16":::
206-
207-
Notice the UI enhancements with these additional comments:
208-
209-
:::image source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-post-remarks.png" alt-text="Swagger UI with additional comments shown.":::
210-
211-
### Data annotations
212-
213-
Mark the model with attributes, found in the <xref:System.ComponentModel.DataAnnotations?displayProperty=fullName> namespace, to help drive the Swagger UI components.
214-
215-
Add the `[Required]` attribute to the `Name` property of the `TodoItem` class:
216-
217-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Models/TodoItem.cs" highlight="10":::
218-
219-
The presence of this attribute changes the UI behavior and alters the underlying JSON schema:
220-
221-
:::code language="json" source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-schemas-todoitem.json" range="2-23" highlight="3-5":::
222-
223-
Add the `[Produces("application/json")]` attribute to the API controller. Its purpose is to declare that the controller's actions support a response content type of *application/json*:
224-
225-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Controllers/TodoController.cs" id="snippet_ClassDeclaration" highlight="3":::
226-
227-
The **Media type** drop-down selects this content type as the default for the controller's GET actions:
228-
229-
:::image source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-get-media-type.png" alt-text="Swagger UI with default response content type":::
230-
231-
As the usage of data annotations in the web API increases, the UI and API help pages become more descriptive and useful.
232-
233-
### Describe response types
234-
235-
Developers consuming a web API are most concerned with what's returned&mdash;specifically response types and error codes (if not standard). The response types and error codes are denoted in the XML comments and data annotations.
236-
237-
The `Create` action returns an HTTP 201 status code on success. An HTTP 400 status code is returned when the posted request body is null. Without proper documentation in the Swagger UI, the consumer lacks knowledge of these expected outcomes. Fix that problem by adding the highlighted lines in the following example:
238-
239-
:::code language="csharp" source="~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Controllers/TodoController.cs" id="snippet_Create" highlight="17-18,20-21":::
240-
241-
The Swagger UI now clearly documents the expected HTTP response codes:
242-
243-
:::image source="~/tutorials/web-api-help-pages-using-swagger/_static/v6-swagger-post-responses.png" alt-text="Swagger UI showing POST Response Class description 'Returns the newly created Todo item' and '400 - If the item is null' for status code and reason under Response Messages.":::
244-
245-
Conventions can be used as an alternative to explicitly decorating individual actions with `[ProducesResponseType]`. For more information, see <xref:web-api/advanced/conventions>.
246-
247-
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.
248-
249-
### Customize the UI
250-
251-
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.
252-
253-
Enable Static File Middleware:
254-
255-
[!code-csharp[](~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Snippets/Program.cs?name=snippet_MiddlewareStaticFiles&highlight=2)]
256-
257-
To inject additional CSS stylesheets, add them to the project's *wwwroot* folder and specify the relative path in the middleware options:
258-
259-
[!code-csharp[](~/tutorials/web-api-help-pages-using-swagger/samples/6.x/SwashbuckleSample/Snippets/Program.cs?name=snippet_MiddlewareInjectStylesheet&highlight=5)]
260-
261-
## Additional resources
262-
263-
* [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/tutorials/web-api-help-pages-using-swagger/samples/) ([how to download](xref:index#how-to-download-a-sample))
264-
* [Improve the developer experience of an API with Swagger documentation](/training/modules/improve-api-developer-experience-with-swagger/)
265-
266-
:::moniker-end
267-
26813
[!INCLUDE[](~/tutorials/getting-started-with-swashbuckle/includes/getting-started-with-swashbuckle8.md)]

aspnetcore/tutorials/web-api-help-pages-using-swagger.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ author: RicoSuter
44
description: This tutorial provides a walkthrough of adding Swagger to generate documentation and help pages for a web API app.
55
ms.author: wpickett
66
ms.custom: mvc
7+
monikerRange: ">= aspnetcore-3.1 <= aspnetcore-8.0"
78
ms.date: 4/25/2024
89
uid: tutorials/web-api-help-pages-using-swagger
910
---
1011
# ASP.NET Core web API documentation with Swagger / OpenAPI
1112

12-
[!INCLUDE[](~/includes/not-latest-version.md)]
13-
1413
:::moniker range=">= aspnetcore-8.0"
1514

1615
By [Christoph Nienaber](https://twitter.com/zuckerthoben) and [Rico Suter](https://blog.rsuter.com/)

0 commit comments

Comments
 (0)