Ask Your Question
4

What are the steps for utilizing Swashbuckle to give Model documentation and Example Value?

asked 2021-05-28 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-12-20 13:00:00 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-28 11:00:00 +0000

Seen: 9 times

Last updated: Dec 20 '22