Replace Swashbuckle UI completely

为君一笑 提交于 2019-11-28 23:37:14

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");
    });
  }
}
Nguyễn Top

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)

I was able to use the latest swagger-ui by following the simple steps here ,https://swagger.io/docs/swagger-tools/#swagger-ui-documentation-29

  1. Download the swagger-ui from GitHub
  2. Extract and copy the dist (rename folder to swagger) folder and include it in the project
  3. Modify index.html in the dist folder to point to your swagger doc path (which off course is generated by Swashbuckle)

For .NET Core projects the solution differs a bit from the (correct) answer of @OndrejSvejdar:

After adding index.html as an embedded resource, you have to add the following line to app.UseSwaggerUI() in your Startup.cs...

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //...

        app.UseSwaggerUI(c =>
        {
            c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Your.Default.Namespace.Subfolder.Swagger_Custom_index.html");
        });

        //...
    }

All details of the process can be found here: https://stackoverflow.com/a/51043251/430742

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!