1 | initial version |
Here are the steps to utilize Swashbuckle to give Model documentation and Example Value:
Swashbuckle.AspNetCore
in your .NET Core project.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
.
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.
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.
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.