Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are the steps to utilize Swashbuckle to give Model documentation and Example Value:

  1. Install NuGet package Swashbuckle.AspNetCore in your .NET Core project.
  2. In the Startup.cs file, add the following code in the ConfigureServices method:

    services.AddSwaggerGen(c => 
    {
       c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Documentation", Version = "v1" });
    });
    

    This will create a new Swagger document with the given Title and Version.

  3. In the same method, add the following code:

    services.AddSwaggerGen(c => 
    {
       c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Documentation", Version = "v1" });
       c.EnableAnnotations();
    });
    

    This will enable annotation support for Swagger, which is required to give Model documentation and Example Value.

  4. In your Model class, add annotations to provide documentation and Example Value. For example:

    public class Person 
    {
       [SwaggerSchema(Description = "The person's name", Example = "John Doe")]
       public string Name { get; set; }
    
       [SwaggerSchema(Description = "The person's age", Example = 25)]
       public int Age { get; set; }
    }
    

    In this example, we have provided documentation in the Description property, and an example value in the Example property.

  5. Run your application, and navigate to /swagger in your browser. You should see the Swagger UI with your API documentation, including Model documentation and Example Value.