DEV Community

Rod Falanga
Rod Falanga

Posted on

I don't understand why my Minimal API doesn't bring up swagger

I have a Blazor Web Application I've been working on (Visual Studio 2022 and .NET 9). This application had 4 VS projects in the solutions, one of them was a Minimal API app. Due to problems I encountered with that configuration, I have decided to migrate the Minimal API project out of that VS solution, into a new VS solution that has only one VS project, which is the Minimal API from the other VS solution.

However, I've found that when debugging the new VS solution it does not bring up Swagger. I've tried using GitHub Copilot, but that just resulted in following Copilot running along a rabbit trail. So, I'm posting the Program.cs file from the new VS solution. Please review and tell me what I'm doing wrong.

using AutoMapper;
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://this.is.not.real/"),
    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.API v1");
    });
}

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

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();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)