My Minimal API app will not launch Swagger; why not?

Falanga, Rod, DOH 400 Reputation points
2025-10-17T19:28:48.3333333+00:00

A while ago I created a new Blazor Web App and put in the Visual Studio solution some projects. One of the projects is a Blazor server-side app with rendermode InterractiveServer. A second project is a Blazor WebAssembly app, where the team I'm on intends to put most of the data collection pages.

A third project is a Minimal API project which I had intended to be utilized by both the server-side and client-side projects, by referencing the Minimal API project using a project reference from both the server-side and client-side projects. That, I now realize, was a serious mistake.

So, I decided to create a new Visual Studio solution and migrate the Minimal API project out of the first VS solution, into the new VS solution. Another thing I'm trying to do with this new solution is utilize Swagger. However, now that I've got the new VS solution created and fixed all of the dependencies in it, I've found that when debugging this new project, it never brings up Swagger. I tried using the free version of GitHub Copilot to resolve this issue. However, after modifying 4 files with multiple interactions with GitHub Copilot, I was no nearer to resolving the issue than I was before. In my experience, once GitHub Copilot starts down a rabbit trail, it will go on modifying more a more files, resolving issues it created rather than the issue I need solved. Therefore, I am going to post here the contents of the Program.cs file in the Minimal API project, minus several endpoints which I don't believe will add much to this discussion. I'm hoping someone here can point me to the mistake I've made. Or, if I should supply another file for reference, please let me know. Here's the contents of the Program.cs file:

using Azure.Identity;
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using FPTimetrackCore.DataActionsAPI;
using FPTimetrackCore.DataActionsAPI.DTO;
using FPTimetrackCore.DataActionsAPI.Model;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Net;
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// -- Key Vault Configuration --
var isDev = builder.Environment.IsDevelopment();
var vaultUri = Environment.GetEnvironmentVariable("VaultUri");

if(!isDev && !string.IsNullOrWhiteSpace(vaultUri))
{
    builder.Configuration.AddAzureKeyVault(new Uri(vaultUri), new DefaultAzureCredential());
}

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "FPTimetrackCore.DataActionsAPI",
        Version = "v1"
    });
});
builder.Services.AddAutoMapper(typeof(MappingConfig));


builder.Configuration.AddAzureKeyVault(
    new Uri($"https://MYKEYVAULT.vault.azure.net/"),
    new DefaultAzureCredential());

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "FPTimetrackCore.DataActionsAPI v1");
    });
}

app.UseHttpsRedirection();

static SaveData GetConnectionObject(bool prodConn)
{
    IConfiguration configuration = null;
    if (prodConn)   // Is this really the best way to do this?
    {
        configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables()
            .Build();
    }
    else
    {
        configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.Development.json")
            .AddEnvironmentVariables()
            .Build();
    }
    var lib = new SaveData(configuration);
    return lib;
}

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "FPTimetrackCore.nEWAPI v1");
    });
}

const int MAX_MONTHS = -3; // This is the number of months to go back from today, from the original Family Planning Timetrack application.

// And I didn't put it as an API route parameter.
app.MapGet("/api/getphdusers/{prodConn}/{Active}", (IConfiguration _config, IMapper _mapper, bool prodConn = true, bool Active = true) =>
{
    TimetrackContext ctx = GetDbContext(_config, prodConn);
    IQueryable<PhdUser> phdUsers = null;
    if (Active)
    {
        phdUsers = ctx.PhdUsers.Where(a => a.PhdUserStatus == "Active");
    }
    else
    {
        phdUsers = ctx.PhdUsers;
    }
    IOrderedQueryable<PhdUser> users = phdUsers.OrderBy(o => o.PhdUserName);
    var phdUserArray = users.ToArray();
    var PhdUsersDTOArray = _mapper.Map<PhdUserDTO[]>(phdUserArray);
    APIResponse response = new()
    {
        IsSuccess = true,
        Result = PhdUsersDTOArray,
        StatusCode = System.Net.HttpStatusCode.OK
    };
    return Results.Ok(response);
})
    .WithName("GetPhdUsers")
    .WithOpenApi()
    .Produces<APIResponse>(StatusCodes.Status200OK);

// Other endpoints go here, but omitted for brevity.

app.Run();


ccc

Developer technologies | ASP.NET | ASP.NET API
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Omkara Varshitha Kunapalli (INFOSYS LIMITED) 1,350 Reputation points Microsoft External Staff
    2025-10-21T08:53:05.3766667+00:00

    Hello Thanks for reaching out!

    Step 1: Remove Duplicate Swagger Setup

    You currently have two blocks of Swagger configuration under if (app.Environment.IsDevelopment()). This can cause conflicts.

     Fix: Keep only one block like this:

    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "FPTimetrackCore.DataActionsAPI v1");
        });
    }
    Step 2: Check Your Environment

    Swagger is only enabled in Development mode.

    Fix: Make sure you're running the app in Development mode:

    • Open launchSettings.json
    • Confirm "ASPNETCORE_ENVIRONMENT": "Development" is set
      Step 3: Set Launch URL to Swagger

    Swagger won’t open automatically unless you tell Visual Studio to launch it.

     Fix: In launchSettings.json, set:

     

    "launchUrl": "swagger"

    Step 4: Make Sure the Project Is Set to Start

     Fix: In Visual Studio:

    • Right-click your Minimal API project
    • Select "Set as Startup Project"

    Once you’ve made these changes, rebuild and run the project. Swagger should now launch in your browser.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.