【问题标题】:Replace Swashbuckle UI completely完全替换 Swashbuckle UI
【发布时间】:2015-10-17 07:35:46
【问题描述】:
现在我正在开发一个 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 实现,以便我可以使用该仓库中的解决方案?
【问题讨论】:
标签:
asp.net
asp.net-web-api
swagger
swashbuckle
【解决方案1】:
当然可以 - 分两步。
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 => {
// 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");
});
}
}
【解决方案2】:
您只需下载.zip 文件夹,解压缩并包含到您的项目中。
在SwaggerConfigre.cs 中,您不再需要配置。
只需将模板放入文件夹Swagger,当您访问{domain}/swagger 时,它会点击index.html。 (不需要将构建操作更改为嵌入式资源,内容很好)
【解决方案4】:
对于 .NET Core 项目,该解决方案与@OndrejSvejdar 的(正确)答案略有不同:
将 index.html 添加为嵌入资源后,您必须将以下行添加到 Startup.cs 中的 app.UseSwaggerUI()...
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//...
app.UseSwaggerUI(c =>
{
c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Your.Default.Namespace.Subfolder.Swagger_Custom_index.html");
});
//...
}
可以在此处找到该过程的所有详细信息:https://stackoverflow.com/a/51043251/430742