Script Bytes

Tutorials and Tips for Angular, .Net, and More

Swagger 404 When Deployed

Jeff F
Jeff F
Cover Image for Swagger 404 When Deployed

I recently put together a very quick proof of concept for a .Net Core 6 API. When I deployed it, in our case to Google Cloud Run, the swagger page was throwing a 404. It sadly took me and a coworker longer than I care to admit to find the problem.

The Problem

When .Net generates your API, it adds this little line in the Program.cs which was the gotcha in our case:

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

This prevents the Swagger page from generating on the non-dev server. That was it! If you want Swagger to work on your production API, remove this if statement.

Hopefully this helps someone out!