找不到Swashbuckle.AspNetCore SwaggerOperation属性

时间:2018-08-22 19:59:28

标签: asp.net-core swashbuckle

我试图使用由Swashbuckle.AspNetCore(3.0.0)帮助创建的Autorest和Swagger文档来生成REST API客户端。

生成的大写文档似乎是正确的,只是操作名称不是很好。

"/api/Addresses/{id}": {
      "get": {
        "tags": [ "Address" ],
        "operationId": "ApiAddressesByIdGet",
        "consumes": [],
        "produces": [],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "type": "string",
            "format": "uuid"
          }
        ],
        "responses": { "200": { "description": "Success" } }
      },

我在很多文章中以及在SwashBuckle.AspNetCore的官方文档中都看到过,我可以使用属性来装饰控制器方法,如下所示:

 [HttpGet]
 [Produces("application/json")]
 [ProducesResponseType((int)HttpStatusCode.NotFound)]
 [ProducesResponseType(typeof(List<AddressDto>), (int)HttpStatusCode.OK)]
 [SwaggerOperation("GetAllAdresses")]
 public async Task<IActionResult> GetAllAsync()
 {
   ....
 }

不幸的是,我遇到一个错误:SwaggerOperationAttribute找不到!

我验证了已安装的nuget软件包,它们是:

  • SwashBuckle.AspNetCore.Swagger(3.0.0)
  • SwashBuckle.AspNetCore.SwaggerGen(3.0.0)
  • SwashBuckle.AspNetCore.SwaggerUI(3.0.0)

有人可以帮助我吗?请

2 个答案:

答案 0 :(得分:16)

我今天碰到了这个。我需要添加以下刚刚为V3.0.0添加的nuget程序包:

Swashbuckle.AspNetCore.Annotations

描述了重大更改here

请注意,您需要将以下内容添加到Startup.cs或Swagger扩展名:

AbleToShoot == true

答案 1 :(得分:0)

另一种选择是为Name过滤器设置HttpGet属性,如下所示:

[HttpGet(Name = "GetAllAdresses")]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(List<AddressDto>), (int)HttpStatusCode.OK)]
[SwaggerOperation("GetAllAdresses")]
public async Task<IActionResult> GetAllAsync()
{
   ....
}
相关问题