如何查看 Azure API 应用程序的 swagger ui

How can view the swagger ui for Azure API app

我正在 Visual Studio 2015 上创建一个 Azure API 应用程序。当我点击浏览并重定向到 http://localhost:3012/ 如果我将 swagger 添加到 url 什么都没发生:http://localhost:3012/swagger

看来我需要添加 /docs/v1 以获得完整地址:http://localhost:3012/swagger/docs/v1。当我添加 /swagger 以加载 swagger 页面时,不应该有一个自动 URL 路由。 另外,我只能查看 json 架构,如果我浏览到 UI http://localhost:3012/swagger/ui 页面不会加载。

API 应用构建成功。有什么遗漏吗?

在您的 WebAPI 项目配置中(SwaggerConfig.cs 在文件夹 App_Start 中),您必须启用 Swagger UI。它被注释掉为默认行为。

只需打开配置,搜索:

                /*
                    })
                .EnableSwaggerUi(c =>
                    {
                */

并禁用其上下行中的注释

上面的 Pedro 为启用 SwaggerUi 提供了完美的答案。 至于你关于 URL "swagger/docs/v1"

的问题

这是 Swashbuckle 用于 return Swagger 2.0 JSON API

元数据的默认 URL

SwaggerConfig.cs 文件是在项目中安装 Swashbuckle 包时创建的。您可以在文件夹 "App_Start" 中找到它。它提供了多种配置 Swashbuckle 的方法。我还没有检查您是否可以更改默认 URL 或对其进行 URL 重新路由。

已编辑:

Swagger 文档和 swagger-ui 的默认路由模板分别为 "swagger/docs/{apiVersion}" 和 "swagger/ui/{*assetPath}"。只要提供的模板包含相关的路由参数 - {apiVersion} 和 {*assetPath},您就可以更改这些。 例如:URL 到 swagger-ui 将是 myswag/index.

httpConfiguration .EnableSwagger("docs/{apiVersion}/swagger", c => c.SingleApiVersion("v1", "A title for your API"))

.EnableSwaggerUi("myswag/{*assetPath}");

您可以在 GitHub 存储库中阅读更多相关信息:https://github.com/domaindrivendev/Swashbuckle

如果您在 visual studio 中使用模板 Azure API App 创建 API,您只需要 un-comment 几个SwaggerConfig.cs 文件中的行来为您的 API 应用程序激活 Swagger。

  1. 在您的解决方案中查找文件 'SwaggerConfig.cs'

    'VSSolution' > App_Start > SwaggerConfig.cs

  2. 查找 EnableSwaggerUi 并删除下面代码行的注释

                /*
                    })
                .EnableSwaggerUi(c =>
                    {
                */
    
  3. 就是这样,不需要其他配置。

    只需 运行 申请: http://localhost:57452/招摇

我遇到了同样的问题,被这些答案误导了一点,尽管我看到它们最终被公认为是完全正确的。

注意以下几点:

  • 不要将 .EnableSwagger 设置误认为是 EnableSwaggerUi SwaggerConfig.cs 中的设置。

    • 前者默认开启,出现在 SwaggerConfig.cs 文件
    • 后者在默认情况下被注释掉并埋在许多其他注释中。我花了很长时间才注意到实际上有 两个 设置。
  • .EnableSwagger 设置会导致 raw JSON 在您的应用程序上发出 url https://yourapp.azurewebsites.net/swagger/docs/v1 - 帮助不大。

  • 虽然 .EnableSwaggerUi 设置导致 Swagger UI 出现在您的 url https://yourapp.azurewebsites.net/swagger