Swashbuckle returns 404 for swagger.json with nothing in the logs
Fix it fast
Most common cause, 1 of 3Swagger middleware is inside the Development-only block
Decide where the document should be public.
// Program.cs
app.UseSwagger();
app.UseSwaggerUI();
# one-off local run (needs .NET SDK 9.0.200 or later)
dotnet run -e ASPNETCORE_ENVIRONMENT=DevelopmentShort answer
A 404 for /swagger/v1/swagger.json with no exception means Swashbuckle never matched the request. Microsoft's sample enables UseSwagger only in Development, so a server running as Production has no route. In an IIS virtual directory, a leading slash in SwaggerEndpoint points at the site root. Fix the environment check, use v1/swagger.json, and match the SwaggerDoc name (Swashbuckle 11).
Which cause do you have?
Request the JSON document directly, at the address the app is really published under, and note the environment line the app prints at start-up. A 404 together with Production points to cause 1. A 200 at the full path while the UI still fails points to cause 2. A 404 even in Development points to cause 3.
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com/myapp/swagger/v1/swagger.json
# start-up log line to look for: "Hosting environment: Production"Then find the line you see in this list and open its fix:
- 1Swagger middleware is inside the Development-only block
The app logs Hosting environment: Production and both /swagger and /swagger/v1/swagger.json return 404 - 2The UI asks for swagger.json at the site root instead of the app's sub-path
/myapp/swagger/v1/swagger.json returns 200, but the UI shows Fetch error Not Found /swagger/v1/swagger.json - 3The document name or route template does not match what the UI requests
404 even in Development, and SwaggerDoc uses a name (v2, MyApi) or a RouteTemplate that differs from the SwaggerEndpoint URL
What does the error look like?
Failed to load API definition.Fetch errorNot Found /swagger/v1/swagger.json
Cause 1 of 3
Swagger middleware is inside the Development-only block
You seeThe app logs Hosting environment: Production and both /swagger and /swagger/v1/swagger.json return 404
Check
swagger.json returns 404 on a server when UseSwagger sits inside if (app.Environment.IsDevelopment()), as in Microsoft's Swashbuckle tutorial. ASP.NET Core falls back to Production when neither ASPNETCORE_ENVIRONMENT nor DOTNET_ENVIRONMENT is set, and Microsoft notes that deployed apps normally run as Production.
grep -n -B2 -A3 'UseSwagger' Program.csFix
Decide where the document should be public. To serve it in every environment, move the two calls out of the if block. To test a deployed build locally as Development, set the environment for one run instead of changing code.
// Program.cs
app.UseSwagger();
app.UseSwaggerUI();
# one-off local run (needs .NET SDK 9.0.200 or later)
dotnet run -e ASPNETCORE_ENVIRONMENT=DevelopmentVerify
The same curl now prints 200, and the browser shows the document's JSON.
curl -s https://api.example.com/swagger/v1/swagger.json | head -c 120Confirmed by learn.microsoft.com
Cause 2 of 3
The UI asks for swagger.json at the site root instead of the app's sub-path
You see/myapp/swagger/v1/swagger.json returns 200, but the UI shows Fetch error Not Found /swagger/v1/swagger.json
Check
Behind an IIS virtual directory or a reverse proxy that adds a path, a SwaggerEndpoint that starts with a slash sends the browser to the true root of the host. Microsoft's tutorial calls out this case: the document lives under the virtual directory, so the root-relative request finds nothing.
grep -n 'SwaggerEndpoint' Program.csFix
Give SwaggerEndpoint a path relative to the UI page, as the Swashbuckle README does. The browser then resolves it under whatever base path the app is served from.
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("v1/swagger.json", "My API V1");
});Verify
Reload /myapp/swagger. The browser's network tab shows the request going to /myapp/swagger/v1/swagger.json with status 200.
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com/myapp/swagger/v1/swagger.jsonConfirmed by learn.microsoft.com
Cause 3 of 3
The document name or route template does not match what the UI requests
You see404 even in Development, and SwaggerDoc uses a name (v2, MyApi) or a RouteTemplate that differs from the SwaggerEndpoint URL
Check
Swashbuckle serves each document at /swagger/{documentName}/swagger.json, where the name is the first argument to SwaggerDoc. Its middleware answers 404 when the requested name is unknown. A custom RouteTemplate moves the JSON, and the UI keeps calling the old address until you change it too.
grep -nE 'SwaggerDoc\(|RouteTemplate|SwaggerEndpoint' Program.csFix
Use one name in all three places. If you changed RouteTemplate, it must still contain {documentName}, and the UI endpoint has to follow the new path.
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
app.UseSwagger(options =>
{
options.RouteTemplate = "api-docs/{documentName}/swagger.json";
});
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/api-docs/v1/swagger.json", "My API V1");
});Verify
The JSON answers at the new address and the UI lists the v1 document without an error box.
curl -s -o /dev/null -w "%{http_code}\n" https://localhost:5001/api-docs/v1/swagger.jsonConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Add a smoke test to the deploy pipeline that requests swagger.json at the published base path and fails on anything but 200.
- Keep the SwaggerDoc name in one constant and build both the RouteTemplate and the SwaggerEndpoint from it.
- Write down in the README which environments expose the API document, so nobody expects it on a Production server.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.