docs/configure-and-customize-redoc.md
Swashbuckle.AspNetCore.ReDocBy default, the Redoc UI will be exposed at /api-docs. If necessary, you can alter this when enabling the Redoc middleware:
<a id='snippet-Redoc-RoutePrefix'></a>
app.UseReDoc(options =>
{
options.RoutePrefix = "docs";
});
<sup><a href='/test/WebSites/DocumentationSnippets/WebApplicationExtensions.cs#L142-L147' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-RoutePrefix' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->By default, the Redoc UI will have a generic document title. You can alter this when enabling the Redoc middleware:
<!-- markdownlint-disable MD031 MD033 --> <!-- snippet: Redoc-DocumentTitle --><a id='snippet-Redoc-DocumentTitle'></a>
app.UseReDoc(options =>
{
options.DocumentTitle = "My API Docs";
});
<sup><a href='/test/WebSites/DocumentationSnippets/WebApplicationExtensions.cs#L149-L154' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-DocumentTitle' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->Redoc ships with its own set of configuration parameters, all described in the Redoc documentation. In Swashbuckle.AspNetCore, most of these are surfaced through the Redoc middleware options:
<!-- markdownlint-disable MD031 MD033 --> <!-- snippet: Redoc-CustomOptions --><a id='snippet-Redoc-CustomOptions'></a>
app.UseReDoc(options =>
{
options.SpecUrl("/v1/swagger.json");
options.EnableUntrustedSpec();
options.ScrollYOffset(10);
options.HideHostname();
options.HideDownloadButton();
options.ExpandResponses("200,201");
options.RequiredPropsFirst();
options.NoAutoAuth();
options.PathInMiddlePanel();
options.HideLoading();
options.NativeScrollbars();
options.DisableSearch();
options.OnlyRequiredInSamples();
options.SortPropsAlphabetically();
});
<sup><a href='/test/WebSites/DocumentationSnippets/WebApplicationExtensions.cs#L156-L174' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-CustomOptions' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->[!NOTE] Using
options.SpecUrl("/v1/swagger.json")multiple times within the sameUseReDoc(...)will not add multiple URLs.
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:
<a id='snippet-Redoc-CustomCSS'></a>
app.UseReDoc(options =>
{
options.InjectStylesheet("/redoc/custom.css");
});
<sup><a href='/test/WebSites/DocumentationSnippets/WebApplicationExtensions.cs#L176-L181' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-CustomCSS' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->It is also possible to modify the theme by using the AdditionalItems property. More information can be found
in the Redoc documentation.
<a id='snippet-Redoc-ModifyTheme'></a>
app.UseReDoc(options =>
{
options.ConfigObject.AdditionalItems = new Dictionary<string, object>
{
// Configured additional options
};
});
<sup><a href='/test/WebSites/DocumentationSnippets/WebApplicationExtensions.cs#L183-L191' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-ModifyTheme' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->To customize the UI beyond the basic options listed above, you can provide your own version of the Redoc index.html page:
<a id='snippet-Redoc-CustomIndexHtml'></a>
app.UseReDoc(options =>
{
options.IndexStream = () => typeof(Program).Assembly
.GetManifestResourceStream("CustomIndex.ReDoc.index.html"); // Requires file to be added as an embedded resource
});
<sup><a href='/test/WebSites/DocumentationSnippets/WebApplicationExtensions.cs#L193-L199' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-CustomIndexHtml' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 --><Project>
<ItemGroup>
<EmbeddedResource Include="CustomIndex.ReDoc.index.html" />
</ItemGroup>
</Project>
[!TIP] To get started, you should base your custom
index.htmlon the default version.
MapReDoc with endpoint routingMapReDoc is an endpoint-routing alternative to UseReDoc. The options API is the same, so all customization examples on this page also apply when using MapReDoc.
<a id='snippet-Redoc-MapReDoc'></a>
app.MapReDoc();
<sup><a href='/test/WebSites/WebApi.Map/Program.cs#L33-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-MapReDoc' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->The main differences are:
UseReDoc adds middleware directly to the request pipeline and returns IApplicationBuilder.
MapReDoc maps ReDoc via endpoint routing and returns IEndpointConventionBuilder.
Because MapReDoc returns an endpoint convention builder, you can attach endpoint metadata/conventions (for example, authorization policies) directly to the mapped ReDoc endpoint.
<a id='snippet-Redoc-MapReDoc-RequireAuthorization'></a>
app.MapReDoc("redoc-auth")
.RequireAuthorization(); // Remember to also add RequireAuthorization to MapSwagger
<sup><a href='/test/WebSites/WebApi.Map/Program.cs#L37-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-Redoc-MapReDoc-RequireAuthorization' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet --> <!-- markdownlint-enable MD031 MD033 -->