Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Swashbuckle UI completely

Right now I'm developing an ASP.Net Web API and using Swashbuckle for its documentation. I know that Swashbuckle use Swagger-UI internally, I know that we can modify the layout by injecting our css or javascript file, even change the layout of index.html.

I found a good themes for Swagger-UI https://github.com/jensoleg/swagger-ui and trying to implement it but can't make it works. Especially when I want to inject bootstrap.js. Is there anyway I can completely change Swashbuckle Swagger UI implementation so I can use the solution from that repo?

like image 331
Martin Valentino Avatar asked Jul 27 '15 07:07

Martin Valentino


People also ask

How do I change the swagger-ui logo?

To replace logo in swagger in api for . net core, you have to add the custom. js file.

Which is embedded version of swagger-ui tool?

In addition to its Swagger 2.0 and OpenAPI 3.0 generator, Swashbuckle also provides an embedded version of the awesome swagger-ui that's powered by the generated Swagger JSON.

What is swashbuckle AspNetCore SwaggerGen?

Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc. AspNetCore.


2 Answers

Sure you can - in two steps.

1) Include file Index.html as Embedded resource in your assembly. For example let's say your web api project is named "Contosco.Api" and Index.html will be located under "/Content/Index.html" in this project.

2) Override swagger UI main html page with your own

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
public class SwaggerConfig 
{
  public static void Register()
  {
    var thisAssembly = typeof(SwaggerConfig).Assembly;
    GlobalConfiguration.Configuration.EnableSwagger(c => {
     // configure swagger
    })
    .EnableSwaggerUi(c => {
      // beware - the Contosco.Api.Content.Index.html has to match your project paths :)
      c.CustomAsset("index", thisAssembly, "Contosco.Api.Content.Index.html");
    });
  }
}
like image 102
Ondrej Svejdar Avatar answered Sep 23 '22 12:09

Ondrej Svejdar


You just download .zip folder, extract and include to your project. In SwaggerConfigre.cs you don't need configure any more. Just put template into folder Swagger and when you access {domain}/swagger it will hit index.html. (Don't need change Build action to Embedded Resource, Content is fine)

like image 21
Nguyễn Top Avatar answered Sep 23 '22 12:09

Nguyễn Top