使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json [英] Add `host`, `basePath` and `schemes` to swagger.json using Swashbuckle Aspnetcore

查看:15
本文介绍了使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用官方文档逐步方法来配置 Swagger UI 并在我的 ASP.NET 核心 API 应用程序中生成 Swagger JSON 文件.

I am using official doc step by step method to configure Swagger UI and generate Swagger JSON file in my ASP.NET core API application.

开始使用 Swashbuckle 和 ASP.NET Core

如果我查看生成的 swagger.json 文件 - 它缺少三个重要属性 hostbasePathschemes

If I look at my generated swagger.json file - it is missing three important properties host, basePath and schemes

请帮助我了解我可以添加哪些代码,以便生成的 swagger.json 将具有以下提到的属性/值.

Please help me understand what piece of code can I add so the swagger.json that gets generated will have following mentioned properties/values.

这是一个理想的 swagger.json - 如果我遵循文档,请注意缺少的 hostbasePathschemes 值我的应用程序中的代码

Here is an ideal swagger.json - give attention to the host, basePath and schemes values which are missing if I follow the documentation code in my application

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Demo API Title"
  },
  "host": "some-url-that-is-hosted-on-azure.azurewebsites.net",
  "basePath": "/api",
  "schemes": ["https"],
  "paths": {
    "/Account/Test": {
      "post": {
        "tags": [
          "Admin"
        ],
        "summary": "Account test method - POST",
        "operationId": "AccountTest",
        "consumes": [],
        "produces": [
          "text/plain",
          "application/json",
          "text/json"
        ],
        "parameters": [],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "type": "boolean"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "NumberSearchResult": {
      "type": "object",
      "properties": {
        "number": {
          "type": "string"
        },
        "location": {
          "type": "string"
        }
      }
    }
  },
  "securityDefinitions": {
    "Bearer": {
      "name": "Authorization",
      "in": "header",
      "type": "apiKey",
      "description": "Authorization. Example: "Authorization: Bearer {token}""
    }
  },
  "security": [
    {
      "Bearer": []
    }
  ]
}

推荐答案

您可以实现并注册您自己的 IDocumentFilter 并在那里设置所需的值.

You can implement and register your own IDocumentFilter and set the desired values there.

public class MyDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.Host = "some-url-that-is-hosted-on-azure.azurewebsites.net";
        swaggerDoc.BasePath = "/api";
        swaggerDoc.Schemes = new List<string> { "https" };
    }
}

然后通过

services.AddSwaggerGen(options =>
{
    options.DocumentFilter<MyDocumentFilter>();
});

这篇关于使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆