asp.net - 完全替换 Swashbuckle UI

标签 asp.net asp.net-web-api swagger swashbuckle

现在我正在开发一个 ASP.Net Web API 并使用 Swashbuckle 作为它的文档。
我知道 Swashbuckle 内部使用 Swagger-UI,我知道我们可以通过注入(inject)我们的 css 或 javascript 文件来修改布局,甚至可以更改 index.html 的布局。

我为 Swagger-UI 找到了一个很好的主题 https://github.com/jensoleg/swagger-ui并试图实现它但无法使其工作。特别是当我想注入(inject) bootstrap.js 时。无论如何我可以完全改变 Swashbuckle Swagger UI 实现,以便我可以使用该 repo 中的解决方案吗?

最佳答案

当然可以 - 分两步。

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");
    });
  }
}

关于asp.net - 完全替换 Swashbuckle UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647635/

相关文章:

c# - Web api - 如何使用 slugs 进行路由?

c# - 什么是 ASP.NET 提供程序?

c# - 如何从 C# ASP.NET 网页调用非托管 C/C++ 代码

c# - 如何在没有回发或更新面板的情况下获取 DropdownList 的 SelectedValue

c# - 将非异步方法(进行网络调用)包装成异步

python - 如何向 bravado 创建的 API 客户端添加授权 header

asp.net - Sitecore 内容交付服务器未清除缓存

c# - 如何在 Asp Net Core RC1 MVC 中使用 token 身份验证添加 "ApiController"

c# - Autorest + Swagger 无法正确生成 ENUM

java - 无法在 Spring 项目中配置 Swagger,获取 File not found 异常