Forum Discussion

qwertyfan2000's avatar
qwertyfan2000
New Member
4 years ago

Two AspNetCore API projects in 1 Visual Studio Solution - Only 1 works with Swashbuckle/Swagger

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" />

 

2) To both projects, in the *.csproj "<PropertyGroup" section, I have added:
<GenerateDocumentationFile>true</GenerateDocumentationFile>

 

3) 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);
});

 

4) 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.

No RepliesBe the first to reply