【问题标题】:How do I use swagger-ui?如何使用 swagger-ui?
【发布时间】:2019-05-09 19:39:03
【问题描述】:

我在我的 web api 项目中使用 Swashbuckle NuGet 包,我试图用这个替换默认的 Swagger UI https://github.com/jensoleg/swagger-ui

但我不断收到此错误消息

<?xml version="1.0" encoding="ISO-8859-1"?>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Embedded resource not found - ReportsAPI.swagger-ui.Index.html</ExceptionMessage>
<ExceptionType>Swashbuckle.SwaggerUi.AssetNotFound</ExceptionType>
<StackTrace> at Swashbuckle.SwaggerUi.EmbeddedAssetProvider.GetEmbeddedResourceStreamFor(EmbeddedAssetDescriptor resourceDescriptor, String rootUrl) at Swashbuckle.SwaggerUi.EmbeddedAssetProvider.GetAsset(String rootUrl, String path) at Swashbuckle.Application.SwaggerUiHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)</StackTrace>
</Error>

我听从了这篇文章的建议 Replace Swashbuckle UI completely

但无法让它工作

我有一个基本的 ASP.Net Web API 2(没有网站或索引 html 的简单 rest api)

所以我从 swagger-ui 的 GitHub 页面下载了 zip 文件,并将 dist 文件夹复制到我的项目中并将其包含为(尝试了 Content 和 Embedded 资源)-并将 dist 文件夹重命名为 swagger-ui

所以我的根项目文件夹现在看起来像

然后我更改了我的 Swagger 配置以添加以下内容

c.CustomAsset("index", thisAssembly, "ReportsAPI.swagger-ui.Index.html");

正确吗?我不知道从这里去哪里

我是否必须将所有其他文件作为嵌入式资源包含在内?还是只是 Index.html?我将其他文件标记为什么,内容?总是复制?

【问题讨论】:

  • 您能否提供一些有关如何用该 UI 替换默认 Swagger UI 的详细信息?你在 GitHub 上有那个项目吗?
  • @HelderSepulveda 我在 github 上没有该项目。我基本上认为您所要做的就是将 swagger-ui 中的“dist”文件夹复制到我的 web api 项目根文件夹中,然后将 swagger 配置指向新的 index.html。我还有什么需要做的吗?
  • 要做到这一点,您需要更改 swashbuckle 以使用该版本的 swagger-ui... 我想您的想法也可以,但需要大量的试验和错误

标签: swagger swagger-ui swashbuckle


【解决方案1】:

这是一种不需要您在项目中嵌入任何内容的替代方法:

https://raw.githack.com/jensoleg/swagger-ui/master/dist/index.html?url=https://swagger-net-test.azurewebsites.net/swagger/docs/V1

如您所见,我的 swagger 规范在 url 参数中提供。
您可以使用 raw.githack.com 或将 dist 文件夹复制到更方便的位置。


现在我要指出的是,这是一个非常旧的 swagger-ui 版本,看起来维护得不好,也许你应该看看:ReDochttps://github.com/Rebilly/ReDoc/blob/master/README.md p>

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 2019-04-24
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多