ASP.NET Core 3.1 C# Swagger and Static Html Content

81 views Asked by At

My current setup is that I am using swagger/swashbuckler in an ASP.NET Core 3.1 Web API. I have multiple version set up using the tags ApiVersion and MapToApiVersion to separate into the appropriate API versions. Then I use a DocInculsionPredicate to separate into the correct documentation version. This is working fine.

I wish to know how I can insert the contents of a static html file beneath the "description" in the information-container div of the Swagger generated documentation.

I've the need for this content to be dependent on the version of the API that is chosen as the information is version specific.

Can anyone help?

1

There are 1 answers

0
Brando Zhang On

According to your description, I suggest you could swagger editor to modify the page.

If you don't want to do it, one possible way is using js to modify the generated html page.

You could create a new js file inside the wwwroot's js folder with below codes:

$(document).ready(function () {
    alert("use this js to modify the html file");
    // Your additional code here...

 });

Then modify the startup.cs codes to inject the js.

        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {

            c.InjectJavascript("https://code.jquery.com/jquery-3.7.1.min.js");
            //var path = Path.Combine(env.WebRootPath, "custom-swagger.js");
            c.InjectJavascript(@"/js/customswagger.js"); ; // Adjust path accordingly
        });

Result:

enter image description here