Swagger 没有为 IActionResult 包装的对象生成模型 [英] Swagger not generating model for object wrapped by IActionResult

查看:25
本文介绍了Swagger 没有为 IActionResult 包装的对象生成模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With the following code, Swaggger UI shows the RegistrationInfo model but not the UserInfo model.

我如何让它生成?

[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 属性.将您的控制器更改为:

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

查看更多这里.

希望这有帮助!

这篇关于Swagger 没有为 IActionResult 包装的对象生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆