gpt4 book ai didi

asp.net-mvc - Swagger 没有为 IActionResult 包装的对象生成模型

转载 作者:行者123 更新时间:2023-12-04 02:54:43 24 4
gpt4 key购买 nike

使用以下代码,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属性。将您的 Controller 更改为:

[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 .

希望这可以帮助!

关于asp.net-mvc - Swagger 没有为 IActionResult 包装的对象生成模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com