Tingle.AspNetCore.Swagger 4.7.0

dotnet add package Tingle.AspNetCore.Swagger --version 4.7.0
NuGet\Install-Package Tingle.AspNetCore.Swagger -Version 4.7.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tingle.AspNetCore.Swagger" Version="4.7.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tingle.AspNetCore.Swagger --version 4.7.0
#r "nuget: Tingle.AspNetCore.Swagger, 4.7.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tingle.AspNetCore.Swagger as a Cake Addin
#addin nuget:?package=Tingle.AspNetCore.Swagger&version=4.7.0

// Install Tingle.AspNetCore.Swagger as a Cake Tool
#tool nuget:?package=Tingle.AspNetCore.Swagger&version=4.7.0

Tingle.AspNetCore.Swagger

[!CAUTION] This documentation for Tingle.AspNetCore.Swagger may not cover the most recent version of the code. Use it sparingly

See https://github.com/tinglesoftware/dotnet-extensions/issues/221

Swagger is a specification for documenting your API and specifies the format (URL, method, and representation) used to describe REST web services. This library is used to generate API documentation as well as add API documentation UI through ReDoc.

The library includes the Swashbuckle.AspNetCore.SwaggerGen library which is a Swagger generator that builds the SwaggerDocument objects directly from your routes, controllers, and models. It is combined with the Swagger endpoint middleware to automatically expose Swagger JSON.

Swashbuckle relies heavily on ApiExplorer (the API metadata layer that ships with ASP.NET Core). If using AddMvcCore for a more pared down MVC stack, you'll need to explicitly add the ApiExplorer service.

In ConfigureServices method of Program.cs, add MvcCore service, the ApiExplorer service, and register the Swagger generator defining one or more Swagger documents.

// Add swagger documents generation
builder.Services.AddSwaggerGen(options =>
{
    options
        .AddDocuments(services.BuildServiceProvider(), ConstStrings.ApiDocsTitle, System.IO.File.ReadAllText("apidesc.md"))
        .AlwaysShowAuthorizationFailedResponse()
        .AlwaysShowBadRequestResponse();

    options.IncludeXmlComments<Program>();
});

In the Configure method of Program.cs, insert middleware to expose the generated Swagger as JSON endpoints.

// Add swagger schema docs
app.MapSwagger(options => options.PreSerializeFilters.Add((swaggerDoc, httpRequest) => swaggerDoc.Host = httpRequest.Host.Value));

In the code snippet above, the SwaggerDocument and the current HttpRequest are passed to the filter thus providing a lot of flexibility. The filter will be executed prior to serializing the document.

ReDoc functionalities are described in the sections below:

ReDoc

ReDoc adds API documentation UI. It can be used to document complex request/response payloads. It also supports nested schemas and displays them in place with the ability to collapse or expand.

This library is used to add ReDoc middleware to the HTTP request pipeline. This adds API documentation UI.

Setup

Add the following code logic to Program.cs

// Add ReDoc services
builder.Services.AddReDoc(o =>
{
    o.DocumentTitle = ConstStrings.ApiDocsTitle;
    o.DefaultDocumentName = $"v{ConstStrings.ApiVersion2}";
});

var app = builder.Build();

// Add API documentation UI via ReDoc
app.MapReDoc();
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.7.0 525 3/25/2024