SimonGoldstone
SimonGoldstone

Reputation: 5216

Swagger/Swashbuckle doc generation only for API controllers within an MVC core app

We have an ASP.NET MVC Core application with regular MVC controllers. In addition, we have a subfolder within the Controllers folder called "API", containing a small number of API controllers.

We're using the .IncludeXmlComments method within .AddSwaggerGen to pick up the XML documentation within our project.

However, it's also picking up the XML for all our regular controllers too.

Is there a way to filter out the 'regular' controllers, or otherwise select only API controllers for inclusion in the swagger documentation?

Upvotes: 4

Views: 1505

Answers (1)

Bruno Martins
Bruno Martins

Reputation: 972

Have a look at DocInclusionPredicate, it should solve your problem 😉

// Startup.cs
services.AddSwaggerGen(options => 
    options.DocInclusionPredicate((docName, apiDesc) =>
    {
        if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;

        // Check if methodInfo is in the right assembly
        // or has the right namespace or version etc.
        bool isMethodIncluded = /* true or false */;

        return isMethodIncluded ;
    });
);

Upvotes: 4

Related Questions