Skip to content

Commit bef2c6e

Browse files
author
Fortinbra
committed
fix: return 404 for unmatched API routes instead of SPA fallback
MapFallback was catching all unmatched routes including /api/* paths, returning 200 OK with index.html. This caused the CI test Parse_EndpointRemoved_ReturnsErrorStatus to fail because the removed /api/v1/import/parse endpoint returned 200 instead of 404. Added a guard to return 404 for requests starting with /api/ so API consumers get proper error responses while SPA routing still works for all other paths.
1 parent 7ec6c43 commit bef2c6e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/BudgetExperiment.Api/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,16 @@ await context.Response.WriteAsJsonAsync(new
214214

215215
// Serve index.html with embedded client config to eliminate startup fetch round-trip.
216216
// Config JSON is injected once at startup and cached for the lifetime of the process.
217+
// Only applies to non-API paths so unmatched API routes return proper 404s.
217218
var configInjectedHtml = BuildConfigInjectedIndexHtml(app);
218219
app.MapFallback(async context =>
219220
{
221+
if (context.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase))
222+
{
223+
context.Response.StatusCode = StatusCodes.Status404NotFound;
224+
return;
225+
}
226+
220227
context.Response.ContentType = "text/html; charset=utf-8";
221228
context.Response.Headers.CacheControl = "no-cache";
222229
await context.Response.WriteAsync(configInjectedHtml);

0 commit comments

Comments
 (0)