Swashbuckle - 回复回复的文字文件?

001*_*001 5 c# rest swagger swashbuckle asp.net-core

Swashbuckle不会生成带有"UserCreateResponse"输出的swagger.json,你如何解决这个问题?

    [HttpPost]
    public async Task<IActionResult> Update([FromBody]UserCreate Request)
    {
        UserCreateResponse response = new UserCreateResponse();

        //do something here

        // returns UserCreateResponse with http status code 200
        return Ok(response);
    }
Run Code Online (Sandbox Code Playgroud)

你不能这样做,因为它不会返回http状态代码,200,400,401等

    [HttpPost]
    public async Task<UserCreateResponse> Update([FromBody]UserCreate Request)
    {
        UserCreateResponse response = new UserCreateResponse();

        //do something here

        // returns UserCreateResponse
        return response;
    }
Run Code Online (Sandbox Code Playgroud)

jps*_*jps 8

另一个变体是SwaggerResponse属性的使用,它还允许提供额外的描述:

[SwaggerResponse(HttpStatusCode.OK, "UserDTO", typeof(UserDTO))]
public async Task<IHttpActionResult> Get([FromODataUri] int key)
{
    var result = await UserRepo.GetAsync(key);
    ...
    return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)

产生如下所示的输出:

在此输入图像描述 在此输入图像描述

也可以省略类型以记录不返回实体的其他状态代码:

[SwaggerResponse(HttpStatusCode.NotFound, "no data found")]
[SwaggerResponse(HttpStatusCode.BadRequest, "requiered request headers not found")]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 不幸的是,“SwaggerResponse”在最新版本的 Swashbuckle.AspNetCore 中已被删除。如果您仍然需要字符串解释,`ProducesResponseType` 是可行的方法,带有可选的 XML 注释(在[此处阅读更多相关信息](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/159))。 (2认同)

Hez*_*zye 6

您可以使用以下属性指定响应类型:

[ProducesResponseType(typeof(UserCreateResponse), 200)]
Run Code Online (Sandbox Code Playgroud)