Skip to content

Add built-in support for Swagger UI #55191

Closed
@joegoldman2

Description

@joegoldman2

Introduction

With #55182, support for generating OpenAPI specification files has been added. However, the related feature to visualize these files, like Swagger UI, hasn't been included (and not planned from what I understood). This proposal aims to suggest the add of Swagger UI support within the framework to make it easier to visualize OpenAPI definitions.

Motivation

Visualizing OpenAPI Specification files is important for a better development experience. Swagger UI, with its easy-to-use interface and interactive documentation, helps developers to explore APIs in a practical way and test them easily (especially during the development phase).
Also, starting with .NET 9, users will migrate to the built-in feature to generate OpenAPI specifications (especially because Swashbuckle.AspNetCore will be removed from the web API template). However, this new feature doesn't cover the full functionality offered by the previous approach for working with OpenAPI (Swashbuckle.AspNetCore or NSwag).

Why Swagger UI?

While alternatives like Redoc exist, Swagger UI stands out due to its widespread adoption, robust feature set, and developer-friendly interface.

API Proposal

// namespace to be defined.

public class SwaggerUIOptions
{
    /// <summary>
    /// Gets or sets a route prefix for accessing the Swagger UI page. Defaults to swagger.
    /// </summary>
    public string RoutePrefix { get; set; } = "swagger";

    /// <summary>
    /// Gets or sets the document title for the Swagger UI page. Defaults to Swagger UI.
    /// </summary>
    public string DocumentTitle { get; set; } = "Swagger UI";
}
namespace Microsoft.Extensions.DependencyInjection;

public static class SwaggerUIServiceCollectionExtensions
{
    public static IServiceCollection AddSwaggerUI(this IServiceCollection services);
    public static IServiceCollection AddSwaggerUI(this IServiceCollection services, Action<SwaggerUIOptions> configure);
    public static IServiceCollection AddSwaggerUI(this IServiceCollection services, IConfiguration configuration);
}
namespace Microsoft.AspNetCore.Builder;

public static class SwaggerUIEndpointRouteBuilderExtensions
{
    public static IEndpointConventionBuilder MapSwaggerUI(this IEndpointRouteBuilder endpoints);
    public static IEndpointConventionBuilder MapSwaggerUI(this IEndpointRouteBuilder endpoints, SwaggerUIOptions options);
}

Usage Examples

var builder = WebApplication.CreateBuilder();

builder.Services.AddSwaggerUI();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapSwaggerUI();
}

app.Run();

Risks

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions