Inject embedded js/css file into Swashbuckle

Viewed 151

One can include js/css by placing them in /wwwroot and then using the InjectJavascript and InjectStylesheet options.

But how can I instead inject a file that exists as an embedded resource? It used to be possible (if I remember correctly), but I can't find out how to do it for the AspNetCore version of Swashbuckle.

(I'm not asking how to embed a file, but rather how to tell Swashbuckle to use it.)

1 Answers

To customize the Swashbuckle UI in asp.net core, as you said, we need to use the SwaggerUIOptions's InjectJavascript and InjectStylesheet options.

For example, I create a Asp.net 6 application with Swashbuckle.AspNetCore 6

enter image description here

Then, add the css and javascript fille in the wwwroot folder. enter image description here

Finally, in the program.cs file, enable Static File Middleware and inject the css and javascript file as below:

app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.InjectStylesheet("/swagger-ui/css/custom.css");
    c.InjectJavascript("/swagger-ui/js/CustomJavaScript.js");
});

The result like this:

enter image description here

Related