Asp.net

完全替換 Swashbuckle UI

  • June 27, 2018

現在我正在開發一個 ASP.Net Web API 並使用 Swashbuckle 作為它的文件。我知道 Swashbuckle 內部使用 Swagger-UI,我知道我們可以通過注入我們的 css 或 javascript 文件來修改佈局,甚至改變 index.html 的佈局。

我為 Swagger-UI <https://github.com/jensoleg/swagger-ui>找到了一個很好的主題並嘗試實現它但無法使其工作。特別是當我想注入 bootstrap.js 時。無論如何我可以完全改變 Swashbuckle Swagger UI 實現,以便我可以使用該回購中的解決方案嗎?

當然可以 - 分兩步。

  1. 在程序集中包含文件 Index.html 作為嵌入式資源。例如,假設您的 Web api 項目名為“Contosco.Api”,Index.html 將位於該項目的“/Content/Index.html”下。

  2. 用你自己的覆蓋 swagger UI 主 html 頁面

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
public class SwaggerConfig 
{
 public static void Register()
 {
   var thisAssembly = typeof(SwaggerConfig).Assembly;
   GlobalConfiguration.Configuration.EnableSwagger(c =&gt; {
    // configure swagger
   })
   .EnableSwaggerUi(c =&gt; {
     // beware - the Contosco.Api.Content.Index.html has to match your project paths :)
     c.CustomAsset("index", thisAssembly, "Contosco.Api.Content.Index.html");
   });
 }
}

引用自:https://stackoverflow.com/questions/31647635