How do I include the XML comments on a Controller class in the Swagger documentation pages

Viewed 2361

I have a controller class (doesn't derive directly from ApiController) that has an XML comment:

/// <summary>
/// The controller groups together all methods related to Trial Subscription Management.
/// </summary>
[RoutePrefix("api/v1")]
public class TrialsController : TraceableApiController
{
...
}

I can see the controller on the Swagger documentation page but the XML comment describing the controller is missing.

enter image description here

Is it possible to include the comment that describes the controller and if so, what do I have to do ?

2 Answers

To get controller level comments to display in the SwaggerUI, you have to add a 2nd bool parameter = true to the IncludeXmlComments() method in .AddSwaggerGen(). Like this:

(.AddSwaggerGen() has been cut down for brevity)


            services.AddSwaggerGen(x =>
            {                

                //Locate the XML file being generated by ASP.NET...
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

                //... and tell Swagger to use those XML comments.
                x.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
            });

Reference: https://github.com/domaindrivendev/Swashbuckle/issues/1083#issuecomment-530471158

Related