Skip to content
Merged
22 changes: 22 additions & 0 deletions src/BootstrapBlazor.Server/Components/Pages/Error.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@page "/error"
@page "/error/{StatusCode:int?}"
@layout EmptyLayout
@inject NavigationManager NavigationManager
@using Microsoft.AspNetCore.Http

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
Expand All @@ -13,3 +17,21 @@
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>

@code {
[Parameter]
public int? StatusCode { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnParametersSet()
{
base.OnParametersSet();

if (StatusCode is null || StatusCode == StatusCodes.Status404NotFound)
{
NavigationManager.NavigateTo("/");
}
}
}
12 changes: 0 additions & 12 deletions src/BootstrapBlazor.Server/Components/Pages/NotFound.razor

This file was deleted.

2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Router AppAssembly="@typeof(Program).Assembly" NotFoundPage="typeof(Pages.NotFound)">
<Router AppAssembly="@typeof(Program).Assembly" NotFoundPage="typeof(Pages.Error)">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/print-view"
@layout PrintLayout
@layout EmptyLayout

<section class="container py-3">
<div class="text-center print-header">
Expand Down
13 changes: 12 additions & 1 deletion src/BootstrapBlazor.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// MVC 异常处理页面
app.UseExceptionHandler("/error", createScopeForErrors: true);
}

// 增加上传目录静态资源文件
Expand All @@ -41,6 +42,9 @@
// 增加 llms 静态资源文件
app.UseLLMsStaticFiles();

// 增加状态码页面重定向
app.UseStatusCodePagesWithReExecute("/error/{0}", createScopeForStatusCodePages: true);

// 增加静态资源文件端点映射
app.UseRouting();

Expand All @@ -62,4 +66,11 @@
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();

app.MapFallback(context =>
{
// MVC NOT FOUND 页面重定向到首页
context.Response.Redirect("/");
return Task.CompletedTask;
});

app.Run();