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

Swagger not generating model for object wrapped by IActionResult

使用以下代码,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();
    }
}

您需要使用 ProducesResponseType 属性。将您的控制器更改为:

[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();
    }
}

查看更多here.