Swagger 没有为 IActionResult 包装的对象生成模型

Kir*_*eed 6 asp.net-mvc swagger swagger-ui .net-core

使用以下代码,Swaggger UI 显示 RegistrationInfo 模型,但不显示 UserInfo 模型。

我如何让它生成?

[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api")]

public class UserController : Controller
{

    [HttpPost("RegisterUser")]
    public  IActionResult RegisterUser([FromBody] RegistrationInfo info)
    {
        UserInfo data =    UserData.RegisterUser(info);
        if (data != null)
        {
            return Ok(data);
        }
        return NoContent();
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*.ZA 12

You need to use the ProducesResponseType attribute. Change your controller to this:

[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api")]
public class UserController : Controller
{
    [ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)]
    [HttpPost("RegisterUser")]
    public IActionResult RegisterUser([FromBody] RegistrationInfo info)
    {
        UserInfo data = UserData.RegisterUser(info);
        if (data != null)
        {
            return Ok(data);
        }

        return NoContent();
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处查看更多信息

希望这可以帮助!