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

标签 asp.net-mvc .net-core swagger swagger-ui

使用以下代码,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/

相关文章:

c# - 如何设置cookie值?

c# - 从 Nuget 包中自动提取 native 和托管 DLL

json - Asp .Net Core Project.json 文件

swagger - Microsoft 是否提供 Graph 的 swagger 文件?

javascript - 用JQuery在div中插入html删除Anchor标签

asp.net-mvc - 如何在ASP.NET MVC中创建旋转广告 block ?

html - 使用 CSS 动态调整表格中的 div 大小

visual-studio - 如何在VS2017中查看dotnet cli版本?

swagger - 使用 swagger 时,如何让 Azure API 管理记录请求的主体?

UriTemplate 中使用的 Azure API 管理模板参数必须在操作中定义,反之亦然