0
votes

I have a Visual Studio 2019 solution. It contains 2 AspNetCore 2.2 API projects, as well as some other required projects.

Both API projects have their API base controllers derive from the Microsoft.AspNetCore.MvcController.Controller class. (I have also tried the base class ControllerBase, but this made no difference in my issue)

  1. To both projects, I have added the following, using the NuGet package manager:
<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />
  1. To both projects, in the *.csproj "<PropertyGroup" section, I have added: <GenerateDocumentationFile>true</GenerateDocumentationFile>

  2. To both projects, I have added the following to startup.cs "ConfigureServices(IServiceCollection services)" method:

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "ToDo API",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "My Name",
Email = string.Empty,
Url = new Uri("https://example.com/twitter"),
},
License = new OpenApiLicense
{
Name = "Use under MIT",
Url = new Uri("https://example.com/license"),
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath, true);
});
  1. To both projects, I have added the following to startup.cs "Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)" method:
app.UseDeveloperExceptionPage();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.DocumentTitle = "Todo APIs";
c.DocExpansion(DocExpansion.None);
c.RoutePrefix = string.Empty;
});

One of the projects correctly creates the Swagger UI "index.html" and "swagger.json" files, but the other project does not. It simply acts as it did before adding Swashbuckle/Swagger UI support.

In the API project that is not working with Swagger, the code within the "services.AddSwaggerGen(c =>" code block is not being processed. Is this my issue? If so, what would cause the code block to not execute?

I am new to Swashbuckle/Swagger, so any advice is appreciated.

1

1 Answers

0
votes

To answer my own question:

The .NetCore project that properly supported the Swagger UI had the following code block in an app.UseExceptionHandler() block in the Startup.cs file: `app.UseSwagger(); app.UseSwaggerUI(c => {

c.SwaggerEndpoint("/swagger/v1/swagger.json", " API V1");
                
c.SwaggerEndpoint("/swagger/v2/swagger.json", " API V2");
 `enter code here`               c.DocumentTitle = "API";
c.DocExpansion(DocExpansion.None);
        `enter code here`        c.RoutePrefix = string.Empty;
            });

app.UseMvc();`

Once I replaced the existing "app.Run()" and replaced it with the app.UseExceptionHandler() around the code in my second project, Swashbuckle created the Swagger UI for me to use.