C
C#2y ago
PjotrH

❔ ✅ Swagger page doesn't load in fresh generated 7.0 .net webapi

The API itself works, I can create, query, and access new endpoints in the browser. But the swagger UI either isn't hosted or generated at all (404 not found) My only hint is this warning when I try to load the page: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect ...Which disappears when I comment out app.UseHttpsRedirection(); in Program.cs. Anyone come across this before? Maybe someone could share their config or program.cs?
35 Replies
V0FBU1VM
V0FBU1VM2y ago
The application doesn't seem to be freshly generated. There have been some modifications. Try this
app.UseSwagger();

app.UseSwaggerUI();
app.UseSwagger();

app.UseSwaggerUI();
and navigate to localhost:port/swagger when running.
PjotrH
PjotrHOP2y ago
Yes, c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); on line 22 was added in an attempt at troubleshooting. Removed it again, same result on http://localhost:5132/swagger
PjotrH
PjotrHOP2y ago
PjotrH
PjotrHOP2y ago
here's my launchsettings for good measure
Pobiega
Pobiega2y ago
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
this is the program.cs for a freshly generated web api if you remove your alterations to SwaggerGen and SwaggerEndpoint, does that help?
PjotrH
PjotrHOP2y ago
Nope, same result. Note that I've already tried the fresh version, my alterations have been done to troubleshoot.
Pobiega
Pobiega2y ago
very weird, since the fresh template works
PjotrH
PjotrHOP2y ago
I can try to generate another new project
Pobiega
Pobiega2y ago
yeah just dotnet new webapi -n DeleteMe and run it straight up, not a single line of code modified
PjotrH
PjotrHOP2y ago
will do
Pobiega
Pobiega2y ago
if that doesnt work, something is very weird
PjotrH
PjotrHOP2y ago
DeleteMe? what's this
Pobiega
Pobiega2y ago
just the name of the new project 🙂
PjotrH
PjotrHOP2y ago
oh lol
Pobiega
Pobiega2y ago
-n gives it a name via the CLI and creates it in a new subfolder instead of generating the project where you are
PjotrH
PjotrHOP2y ago
still the same Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect. warning when trying to access root url and http://localhost:5113/swagger
Pobiega
Pobiega2y ago
well, you shouldn't be accessing it on HTTP how are you running it?
PjotrH
PjotrHOP2y ago
dotnet run
PjotrH
PjotrHOP2y ago
it defaults to http apparently
PjotrH
PjotrHOP2y ago
Pobiega
Pobiega2y ago
okay. you can make it use https by default by re-ordering the profiles - or use dotnet run --launch-profile https
PjotrH
PjotrHOP2y ago
this is with https in url that worked but still the same 404 in browser
Pobiega
Pobiega2y ago
https uses a different port make sure you are accessing the correct url
Pobiega
Pobiega2y ago
PjotrH
PjotrHOP2y ago
PjotrH
PjotrHOP2y ago
PjotrH
PjotrHOP2y ago
Pobiega
Pobiega2y ago
Don't take this the wrong way, but you must be doing something wrong :p are you sure you dont have another instance of the program running already, causing binding failures? or another app using those ports
PjotrH
PjotrHOP2y ago
Not that I know of. Gonna make sure brb rebooting Nah still nothing only thing I can think of now is that I had to manually set my environment variable for the 7.0 SDK to generate a project setting it to C:\Program Files\dotnet\sdk\7.0.203\Sdks after installing 7.0 sdk but I can generate the project so clearly that works Noticing something else in my env variables: %USERPROFILE%.dotnet\tools --- C:\Users\peter.dotnet doesn't have any tools folder idk if that's relevant
Pobiega
Pobiega2y ago
probably not for this tools is like a "plugin" for the dotnet cli
PjotrH
PjotrHOP2y ago
gotcha
Pobiega
Pobiega2y ago
Can you jump into #dev-vc-1 and share screen btw?
PjotrH
PjotrHOP2y ago
sure
Pobiega
Pobiega2y ago
Solved - DOTNET_ENVIRONMENT was set to an unexpected value which caused app.Environment.IsDevelopment() to say false
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server