Search code examples
c#asp.net-coreswaggerasp.net-core-webapi

How to add a swagger example to the fields in the asp.net core?


Based on the Swagger documentation I must be able to add some examples for my class fields, something like:

enter image description here

But I can't find any way to add these examples to my classes.


Solution

  • You can enable XML Comments and then use the <example> element to define an example. Here's an example of using an <example>:

    public class CompanyValidationResponse
    {
        /// <example>1234</example>
        public int CompanyId { get; set; }
    
        /// <example>Damage, Inc</example>
        public string CompanyName { get; set; }
    }