diff --git a/src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor b/src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor
new file mode 100644
index 00000000000..a5e396cf306
--- /dev/null
+++ b/src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor
@@ -0,0 +1,3 @@
+@page "/error-page"
+
+
ErrorPage
diff --git a/src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor.cs b/src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor.cs
new file mode 100644
index 00000000000..23cf221f0bb
--- /dev/null
+++ b/src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor.cs
@@ -0,0 +1,26 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License
+// See the LICENSE file in the project root for more information.
+// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
+
+namespace BootstrapBlazor.Server.Components.Pages;
+
+///
+/// ErrorPage 组件用于测试全局异常处理功能
+///
+public partial class ErrorPage
+{
+ ///
+ ///
+ ///
+ protected override void OnInitialized()
+ {
+ base.OnInitialized();
+
+ var a = 1;
+ var b = 0;
+
+ // 这里会抛出异常
+ var c = a / b;
+ }
+}
diff --git a/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor b/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor
index 1944521cfbd..6bdb3ac42b5 100644
--- a/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor
+++ b/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor
@@ -72,4 +72,8 @@
+
+
+
+
diff --git a/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor.cs
index ed08275a408..e2399a87d1f 100644
--- a/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor.cs
+++ b/src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor.cs
@@ -10,6 +10,10 @@ namespace BootstrapBlazor.Server.Components.Samples;
///
public partial class GlobalException
{
+ [Inject]
+ [NotNull]
+ private NavigationManager? NavigationManager { get; set; }
+
[Inject]
[NotNull]
private SwalService? SwalService { get; set; }
@@ -19,7 +23,6 @@ public partial class GlobalException
private static void OnClick()
{
- // NET6.0 采用 ErrorLogger 统一处理
var a = 0;
_ = 1 / a;
}
@@ -39,6 +42,12 @@ private Task OnShowDialog() => DialogService.Show(new DialogOption()
Component = BootstrapDynamicComponent.CreateComponent()
});
+ private Task OnGotoPage()
+ {
+ NavigationManager.NavigateTo("/error-page");
+ return Task.CompletedTask;
+ }
+
///
/// 获得属性方法
///
diff --git a/src/BootstrapBlazor.Server/Locales/en-US.json b/src/BootstrapBlazor.Server/Locales/en-US.json
index cf04c51a796..151124684e9 100644
--- a/src/BootstrapBlazor.Server/Locales/en-US.json
+++ b/src/BootstrapBlazor.Server/Locales/en-US.json
@@ -1725,7 +1725,9 @@
"Block2Intro": "Set custom exception handling logic by setting OnErrorHandleAsync callback method",
"DialogTitle": "In Dialog",
"DialogIntro": "Click the button to pop up a pop-up window. The button in the pop-up window triggers an exception and the error is displayed in the pop-up window",
- "DialogText": "Popup"
+ "DialogText": "Popup",
+ "PageErrorTitle": "Page",
+ "PageErrorIntro": "Click the button to navigate to the page where the error occurred during the page life cycle. The error is displayed on the current page and does not affect the menu and the overall page layout."
},
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
"Title": "Global exception",
diff --git a/src/BootstrapBlazor.Server/Locales/zh-CN.json b/src/BootstrapBlazor.Server/Locales/zh-CN.json
index 0b8641c68f5..2fc26579bfb 100644
--- a/src/BootstrapBlazor.Server/Locales/zh-CN.json
+++ b/src/BootstrapBlazor.Server/Locales/zh-CN.json
@@ -1725,7 +1725,9 @@
"Block2Intro": "通过设置 OnErrorHandleAsync 回调方法,设置自定义异常处理逻辑",
"DialogTitle": "弹窗中异常捕获",
"DialogIntro": "点击按钮弹出弹窗,弹窗内按钮触发异常,错误显示在弹窗内",
- "DialogText": "弹窗"
+ "DialogText": "弹窗",
+ "PageErrorTitle": "页面异常捕获",
+ "PageErrorIntro": "点击按钮导航到页面生命周期内出错的页面,错误显示在当前页面,不影响菜单以及整体页面布局"
},
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
"Title": "全局配置",
diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj
index 798950e0ab1..aa2492b9004 100644
--- a/src/BootstrapBlazor/BootstrapBlazor.csproj
+++ b/src/BootstrapBlazor/BootstrapBlazor.csproj
@@ -1,7 +1,7 @@
- 9.7.0
+ 9.7.1-beta01
diff --git a/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs b/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs
index f9b13ed90b5..ebe855b3435 100644
--- a/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs
+++ b/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs
@@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
+using System.Reflection;
namespace BootstrapBlazor.Components;
@@ -71,10 +72,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
var ex = CurrentException ?? _exception;
if (ex != null)
{
- _exception = null;
-
// 处理自定义异常逻辑
- if(OnErrorHandleAsync != null)
+ if (OnErrorHandleAsync != null)
{
// 页面生命周期内异常直接调用这里
_ = OnErrorHandleAsync(Logger, ex);
@@ -83,6 +82,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
// 渲染异常内容
builder.AddContent(0, ExceptionContent(ex));
+
+ // 重置 CurrentException
+ ResetException();
}
else
{
@@ -91,6 +93,16 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
}
}
+ private PropertyInfo? _currentExceptionPropertyInfo;
+
+ private void ResetException()
+ {
+ _exception = null;
+
+ _currentExceptionPropertyInfo ??= GetType().BaseType!.GetProperty(nameof(CurrentException), BindingFlags.NonPublic | BindingFlags.Instance)!;
+ _currentExceptionPropertyInfo.SetValue(this, null);
+ }
+
private Exception? _exception = null;
private RenderFragment ExceptionContent => ex => builder =>