fix(web): render a 404 page for unknown routes (#294)
Heimdall Gitea / Detect CI scope (push) Successful in 5s
Heimdall Gitea / Build and tests (push) Successful in 32m0s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 8m15s
Heimdall Gitea / Build and scan agent image (push) Successful in 9m53s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m42s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 28m29s
Heimdall Gitea / CI policy (push) Successful in 2s
Heimdall Gitea / Detect CI scope (push) Successful in 5s
Heimdall Gitea / Build and tests (push) Successful in 32m0s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 8m15s
Heimdall Gitea / Build and scan agent image (push) Successful in 9m53s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m42s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 28m29s
Heimdall Gitea / CI policy (push) Successful in 2s
Co-authored-by: Arnaud Dartois <admin@tordais.cc>
This commit was merged in pull request #297.
This commit is contained in:
1 parent
5bcd4968a8
commit
38647d3623
3 files changed
+122
No files matched your search
@@ -850,6 +850,55 @@ public partial class Program
|
||||
localizationOptions.RequestCultureProviders.Insert(1, new CookieRequestCultureProvider());
|
||||
app.UseRequestLocalization(localizationOptions);
|
||||
|
||||
// Unknown browser routes have no endpoint and cannot start the interactive
|
||||
// Blazor circuit. Render the same localized NotFound content as a static
|
||||
// 404 document; preserve native API, hub, health, Hangfire, and asset errors.
|
||||
app.UseStatusCodePages(async statusCodeContext =>
|
||||
{
|
||||
var context = statusCodeContext.HttpContext;
|
||||
var path = context.Request.Path;
|
||||
var isNonUiResponse = path.StartsWithSegments("/api")
|
||||
|| path.StartsWithSegments("/hubs")
|
||||
|| path.StartsWithSegments("/healthz")
|
||||
|| path.StartsWithSegments("/hangfire")
|
||||
|| Path.HasExtension(path.Value)
|
||||
|| !context.Request.Headers.Accept.Any(value =>
|
||||
value?.Contains("text/html", StringComparison.OrdinalIgnoreCase) == true);
|
||||
if (context.Response.StatusCode != StatusCodes.Status404NotFound || isNonUiResponse) return;
|
||||
|
||||
var text = context.RequestServices.GetRequiredService<UiText>();
|
||||
var encoder = System.Text.Encodings.Web.HtmlEncoder.Default;
|
||||
var title = encoder.Encode(text["NotFoundPage_Title"]);
|
||||
var message = encoder.Encode(text["NotFoundPage_Body"]);
|
||||
var back = encoder.Encode(text["NotFoundPage_Back"]);
|
||||
context.Response.ContentType = "text/html; charset=utf-8";
|
||||
var document = $$"""
|
||||
<!doctype html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>{{title}}</title>
|
||||
<style>
|
||||
body { margin: 0; background: #101828; color: #f9fafb; font: 16px Inter, system-ui, sans-serif; }
|
||||
main { box-sizing: border-box; max-width: 48rem; margin: 12vh auto; padding: 2rem; }
|
||||
h1 { color: #60a5fa; font-size: 2.5rem; }
|
||||
p { color: #cbd5e1; line-height: 1.6; }
|
||||
a { display: inline-block; margin-top: 1rem; padding: .75rem 1rem; border-radius: .5rem; background: #2563eb; color: white; text-decoration: none; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>{{title}}</h1>
|
||||
<p>{{message}}</p>
|
||||
<a href="/dashboard">{{back}}</a>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
await context.Response.WriteAsync(document);
|
||||
});
|
||||
|
||||
// Hangfire dashboard — Admin uniquement (policy AdminJobs).
|
||||
// MapHangfireDashboard est utilisé car UseHangfireDashboard avec
|
||||
// DashboardOptions.Authorization n'existe pas pour les policies ASP.NET.
|
||||
|
||||
Reference in new issue
Block a user