【问题标题】:In ASP.NET Core webapi how to I add sample requests to swaggerUI when using NSwag.AspNetCore?在 ASP.NET Core Web api 中,如何在使用 NSwag.AspNetCore 时向 swagger UI 添加示例请求?
【发布时间】:2020-11-12 10:09:26
【问题描述】:

我正在使用

<PackageReference Include="NSwag.AspNetCore" Version="13.8.2" />

在一个 asp.net core web api 3.1 项目中,我有兴趣在 swaggerUI 中显示示例请求有效负载。

使用此框架时,我究竟如何添加示例请求?这个框架甚至可能吗?我最好的猜测是向控制器操作添加属性。任何见解在这里表示赞赏。也许这个问题的答案是一个简单的注释,这是我所希望的。

【问题讨论】:

    标签: asp.net-core nswag


    【解决方案1】:

    您的意思是要添加示例数据到操作中吗?如果是,您可以使用:

    /// <example>xxx</example>
    

    这是一个演示:

    示例.cs:

    public class Sample
        {
            /// <example>1</example>
            public int Id { get; set; }
            /// <example>name</example>
            public string Name { get; set; }
            /// <example>address</example>
            public string Address { get; set; }
        }
    

    控制器:

    [ApiController]
        [Route("[controller]")]
        public class ApiController : ControllerBase
        {
            [HttpPost("Index")]
            public Sample Index(Sample sample)
            {
                return sample;
            }
        }
    

    结果:

    【讨论】:

    • 谢谢。正是我想要的
    • 如果我的回答有用,可以标记为回答吗?谢谢。