I am developing a Asp.Net core 3.1 MVC web app with the web API project inside it. Now I want to configure Swagger Documentation for the API project only, So how can I specify in the configurations to use only the web API controllers for documentation?
Configuration for swagger in startup class inside ConfigureServices method is as follows:-
services.AddSwaggerGen(option =>
{
option.SwaggerDoc("v1.0",
new OpenApiInfo
{
Title = "ProjName OpenApi",
Version = "1.0",
//Description = //get from appsettings.json
});
var xmlCommentFileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlCommentFilePath = Path.Combine(AppContext.BaseDirectory, xmlCommentFileName);
option.IncludeXmlComments(xmlCommentFilePath);
});
and the configuration in Configure method is as follows:-
app.UseSwagger(option =>
{
option.RouteTemplate = "docs/{documentname}/swagger.json";
});
app.UseSwaggerUI(option =>
{
option.SwaggerEndpoint("/docs/v1.0/swagger.json", "ProjName OpenApi v1.0");
option.RoutePrefix = "docs/v1.0";
option.DocumentTitle = "ProjName OpenAPI Docs";
});
The issue is that the swagger gen is looking in controllers folder, Admin & Identity Areas for generating docs, but I would rather like to configure it to Use only the controllers in WebApi Folder. All the Controllers or Action methods which has been specified Route Attribute in those controllers also gets listed in the API Docs. How can I exclude those?
Can some one please help me out with this? I'm really stuck here.
PS: I would like to mention that I can not move the API layer into its separate project.
According to you description, I suggest you could try to create a custom filter to check if the controller name is mvc controller and then remove its route.
More details, you could refer to below codes:
Startup.cs ConfigureServices method:
HideInDocsFilter
Result:
Only contains WeatherForecast controller method