Judy007
Judy007

Reputation: 5870

In ASP.NET Core webapi how to I add sample requests to swaggerUI when using NSwag.AspNetCore?

Im using

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

In an asp.net core web api 3.1 project, and Im interested in having sample request payloads display within the swaggerUI.

How exactly can I add sample requests when using this framework? Is this even possible with this framework? My best guess is that adding attributes to controller actions. Any insight is appreciated here. Perhaps the answer for this is a simple annotation, which I am hoping for.

Upvotes: 1

Views: 420

Answers (1)

Yiyi You
Yiyi You

Reputation: 18239

Do you mean you want to add sample data to action?If so,you can use:

/// <example>xxx</example>

Here is a demo:

Sample.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; }
    }

Controller:

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

result: enter image description here

Upvotes: 2

Related Questions