Search code examples
c#swaggerswagger-ui.net-6.0minimal-apis

Add Swagger description to minimal .NET 6 APIs


I have a small project in .NET 6 that contains minimal APIs like that one

app.MapGet("/clients",
     async (IClientRepository repo) =>
     {
          var results = await repo.GetClientsAsync();
          return mapper.Map<IEnumerable<ClientModel>>(results);
     });

In the SwaggerUI I can use this API but I can't find a way to add description to it (although in the project settings I check for creating an API XML documentation).

enter image description here

How can I add the XML comment?


Solution

  • 1. Install package

    dotnet add package Swashbuckle.AspNetCore.Annotations
    

    2. Write code

    // Program.cs
    
    builder.Services.AddSwaggerGen(x =>
    {
      x.EnableAnnotations();
    });
    
    // app.MapGet("/hi", [SwaggerOperation(Summary = "summary001", Description = "description001 `adads`")] () => "Hi");
    
    app.MapGet("/hi", () => "Hi")
      .WithMetadata(new SwaggerOperationAttribute("summary001", "description001"));
    

    Result

    enter image description here