Skip to content

jonashendrickx/Redoc.AspNetCore

Repository files navigation

This project was started because Swashbuckle is no longer being maintained, and will be dropped from .NET templates with the .NET 9 release. This library will guarantee a smooth transition from .NET 8 to .NET 9.

Example

By default the UI will be available on the following path:

BASEURL/api-docs/index.html

Configuration

ReDoc ships with its own set of configuration parameters, all described here https://github.com/Rebilly/ReDoc/blob/master/README.md#redoc-options-object. In Redoc.AspNetCore, most of these are surfaced through the ReDoc middleware options:

app.UseReDoc(c =>
{
    c.SpecUrl("/v1/swagger.json");
    c.EnableUntrustedSpec();
    c.ScrollYOffset(10);
    c.HideHostname();
    c.HideDownloadButton();
    c.ExpandResponses("200,201");
    c.RequiredPropsFirst();
    c.NoAutoAuth();
    c.PathInMiddlePanel();
    c.HideLoading();
    c.NativeScrollbars();
    c.DisableSearch();
    c.OnlyRequiredInSamples();
    c.SortPropsAlphabetically();
});

Using c.SpecUrl("/v1/swagger.json") multiple times within the same UseReDoc(...) will not add multiple urls.

Inject Custom CSS

To tweak the look and feel, you can inject additional CSS stylesheets by adding them to your wwwroot folder and specifying the relative paths in the middleware options:

app.UseReDoc(c =>
{
    ...
    c.InjectStylesheet("/redoc/custom.css");
}

It is also possible to modify the theme by using the AdditionalItems property, see https://github.com/Rebilly/ReDoc/blob/master/README.md#redoc-options-object for more information.

app.UseReDoc(c =>
{
    ...
    c.ConfigObject.AdditionalItems = ...
}

Customize index.html

To customize the UI beyond the basic options listed above, you can provide your own version of the ReDoc index.html page:

app.UseReDoc(c =>
{
    c.IndexStream = () => GetType().Assembly
        .GetManifestResourceStream("YourNamespace.index.html"); // requires file to be added as an embedded resource
});

To get started, you should base your custom index.html on the default version

Using a Content Security Policy (CSP)

You can overwrite the IndexStream property on the ReDocOptions to inject a nonce into the index.html file. This is useful when you are using a Content Security Policy (CSP) that requires a nonce to be added to inline <script> and <style> tags.

options.IndexStream = () =>
{
    // Read the original index.html file
    using var originalStream = originalIndexStreamFactory();
    using var originalStreamReader = new StreamReader(originalStream);
    var originalIndexHtmlContents = originalStreamReader.ReadToEnd();

    // Get the request-specific nonce generated by NetEscapades.AspNetCore.SecurityHeaders
    var requestSpecificNonce = httpContextAccessor.HttpContext.GetNonce();

    // Replace inline `<script>` and `<style>` tags by adding a `nonce` attribute to them
    var nonceEnabledIndexHtmlContents = originalIndexHtmlContents
        .Replace("<script>", $"<script nonce=\"{requestSpecificNonce}\">", StringComparison.OrdinalIgnoreCase)
        .Replace("<style>", $"<style nonce=\"{requestSpecificNonce}\">", StringComparison.OrdinalIgnoreCase);

    // Return a new Stream that contains our modified contents
    return new MemoryStream(Encoding.UTF8.GetBytes(nonceEnabledIndexHtmlContents));
};

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published