-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add MapSwaggerUI and MapReDoc to support endpoint routing #3822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martincostello
merged 11 commits into
domaindrivendev:master
from
Strepto:map-for-swaggerui
Mar 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68b1122
Add MapSwaggerUI support and related tests for endpoint routing
Strepto 361f32e
Refactor EnsureDefaultUrl
Strepto 33e70ac
Simplify launchSettings
Strepto ffb87f3
Fix spelling
Strepto 191edef
Add MapReDoc
Strepto 89e0c1f
Add tests and doc links for MapReDoc and MapSwaggerUI with authorization
Strepto c9a2d70
Apply suggestions from code review
Strepto e31ed79
Separate tests to Swagger and ReDoc tests
Strepto 4672b49
Update mdsnippets
Strepto f930566
Add routePrefix parameter to MapSwaggerUI and MapReDoc
Strepto 3912023
Apply suggestions from code review
Strepto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,3 +244,25 @@ | |
| <!-- markdownlint-enable MD031 MD033 --> | ||
|
|
||
| [swagger-ui]: https://github.com/swagger-api/swagger-ui | ||
|
|
||
| ## Use `MapSwaggerUI` with endpoint routing | ||
|
|
||
| `MapSwaggerUI` is an endpoint-routing alternative to `UseSwaggerUI`. The options API is the same, so all customization examples in this page also apply when using `MapSwaggerUI`. | ||
|
|
||
| ```cs | ||
| app.MapSwagger(); | ||
| app.MapSwaggerUI(options => | ||
| { | ||
| options.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs"); | ||
| }); | ||
| ``` | ||
|
martincostello marked this conversation as resolved.
Outdated
|
||
|
|
||
| The main differences are: | ||
|
|
||
| - **Use**SwaggerUI adds middleware directly to the request pipeline and returns `IApplicationBuilder`. | ||
| - **Map**SwaggerUI maps the Swagger UI via endpoint routing and returns `IEndpointConventionBuilder`. | ||
|
|
||
| - Because `MapSwaggerUI` returns an endpoint convention builder, you can attach endpoint metadata/conventions (for example, authorization policies) directly to the mapped UI endpoint. | ||
|
Strepto marked this conversation as resolved.
Outdated
|
||
| ```csharp | ||
|
Check failure on line 266 in docs/configure-and-customize-swaggerui.md
|
||
| app.MapSwaggerUI().RequireAuthorization(); | ||
| ``` | ||
|
martincostello marked this conversation as resolved.
Outdated
|
||
2 changes: 1 addition & 1 deletion
2
src/Swashbuckle.AspNetCore.SwaggerUI/PublicAPI/PublicAPI.Unshipped.txt
|
martincostello marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| | ||
| static Microsoft.AspNetCore.Builder.SwaggerUIBuilderExtensions.MapSwaggerUI(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Action<Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIOptions> setupAction = null) -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System.Reflection; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.AspNetCore.Http.Json; | ||
|
|
||
| // This exists just to test and verify that the MapSwagger and MapSwaggerUI works as expected. | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.Configure<JsonOptions>( | ||
| options => options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()) | ||
| ); | ||
| builder.Services.AddEndpointsApiExplorer(); | ||
| builder.Services.AddSwaggerGen(options => | ||
| { | ||
| options.SwaggerDoc("v1", new() { Title = "WebApi", Version = "v1" }); | ||
| }); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapSwagger(); | ||
| app.MapSwaggerUI(); | ||
|
|
||
| app.UseHttpsRedirection(); | ||
|
|
||
| string[] summaries = | ||
| [ | ||
| "Freezing", | ||
| "Bracing", | ||
| "Chilly", | ||
| "Cool", | ||
| "Mild", | ||
| "Warm", | ||
| "Balmy", | ||
| "Hot", | ||
| "Sweltering", | ||
| "Scorching", | ||
| ]; | ||
|
|
||
| app.MapGet("/weatherforecast", () => | ||
| { | ||
| var forecast = Enumerable.Range(1, 5).Select(index => | ||
| new { | ||
| Date = DateTime.Now.AddDays(index), | ||
| TemperatureC = Random.Shared.Next(-20, 55), | ||
| Summary = summaries[Random.Shared.Next(summaries.Length)] | ||
| } | ||
| ) | ||
| .ToArray(); | ||
| return forecast; | ||
| }); | ||
|
|
||
| app.Run(); | ||
|
|
||
| namespace WebApi.Map | ||
| { | ||
| /// <summary> | ||
| /// Expose the Program class for use with <c>WebApplicationFactory</c> | ||
| /// </summary> | ||
| public partial class Program | ||
| { | ||
| } | ||
| } |
|
martincostello marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": false, | ||
| "anonymousAuthentication": true, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:21394", | ||
| "sslPort": 44373 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "swagger", | ||
| "applicationUrl": "http://localhost:5205", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "swagger", | ||
| "applicationUrl": "https://localhost:7175;http://localhost:5205", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "launchUrl": "swagger", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <RootNamespace>WebApi.Map</RootNamespace> | ||
| <TargetFrameworks>$(DefaultTargetFrameworks)</TargetFrameworks> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.Annotations\Swashbuckle.AspNetCore.Annotations.csproj" /> | ||
| <ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.SwaggerUI\Swashbuckle.AspNetCore.SwaggerUI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| @WebApi_HostAddress = http://localhost:5205 | ||
|
|
||
| GET {{WebApi_HostAddress}}/weatherforecast/ | ||
| Accept: application/json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.