首页 文章

Swagger / Swashbuckle显示“未知响应类型”

提问于
浏览
0

我正在创建一个C#ASP.NET Core 2.0 REST API,大部分都进展顺利 . 它使用MVC路由来生成REST API . 控制器非常简单 .

// POST: api/Volume/{zoneID}/Set/{volume}
    [HttpPost("{zoneId:int}/[action]/{volume:int}", Name = "Set")]
    public IActionResult Set(int zoneId, int volume)
    {
        return CreateAndSend(strZonesLevel, zoneId, $"{volume:X2}");

    }

使用最新的一切,为AspNetCore 2.3.0安装了Swagger / Swashbuckle,UI提供了API和所有 . SwashBuckle UI运行良好,我可以测试API等 .

唯一的例外是在UI上,响应类型总是以“未知响应类型”的形式返回 .

https://i.stack.imgur.com/6qqBh.jpg

我的类前面有以下属性(所有方法都返回相同的类型)

[Produces("application/json")]
[Route("api/Volume")]
[ProducesResponseType(typeof(ControllerResponseModel), 200)]
[ProducesResponseType(typeof(ControllerResponseModel), 400)]
[ProducesResponseType(typeof(ControllerResponseModel), 500)]

生成的JSON似乎没问题,ControllerResponseModel在定义中,并由Volume API在所有正确的位置引用 . 这是一个子集 .

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "AVController API"
  },
  "paths": {
    "/api/Volume/{zoneId}/Set/{volume}": {
      "post": {
        "tags": ["Volume"],
        "operationId": "ApiVolumeByZoneIdSetByVolumePost",
        "consumes": [],
        "produces": ["application/json"],
        "parameters": [
          {
            "name": "zoneId",
            "in": "path",
            "required": true,
            "type": "integer",
            "format": "int32"
          },
          {
            "name": "volume",
            "in": "path",
            "required": true,
            "type": "integer",
            "format": "int32"
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "type": "array",
              "items": { "$ref": "#/definitions/ControllerResponseModel" }
            }
          },
          "400": {
            "description": "Bad Request",
            "schema": {
              "type": "array",
              "items": { "$ref": "#/definitions/ControllerResponseModel" }
            }
          },
          "500": {
            "description": "Server Error",
            "schema": {
              "type": "array",
              "items": { "$ref": "#/definitions/ControllerResponseModel" }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "ControllerResponseModel": {
      "type": "object",
      "properties": {
        "command": { "type": "string" },
        "message": { "type": "string" },
        "url": { "type": "string" }
      }
    }
  }
}

任何想法为什么UI不会显示返回类型和值?我尝试了很多东西,比如使用gets而不是post以及使用[SwaggerResponse]属性,但结果是一样的 .

1 回答

相关问题