File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ @page " /error-page"
2+
3+ <h3 >ErrorPage</h3 >
Original file line number Diff line number Diff line change 1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the Apache 2.0 License
3+ // See the LICENSE file in the project root for more information.
4+ // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
5+
6+ namespace BootstrapBlazor . Server . Components . Pages ;
7+
8+ /// <summary>
9+ /// ErrorPage 组件用于测试全局异常处理功能
10+ /// </summary>
11+ public partial class ErrorPage
12+ {
13+ /// <summary>
14+ /// <inheritdoc/>
15+ /// </summary>
16+ protected override void OnInitialized ( )
17+ {
18+ base . OnInitialized ( ) ;
19+
20+ var a = 1 ;
21+ var b = 0 ;
22+
23+ // 这里会抛出异常
24+ var c = a / b ;
25+ }
26+ }
Original file line number Diff line number Diff line change 7272 <Button Icon =" fa-solid fa-font-awesome" Text =" @Localizer[" DialogText " ]" OnClick =" OnShowDialog" />
7373</DemoBlock >
7474
75+ <DemoBlock Title =" @Localizer[" PageErrorTitle " ]" Introduction =" @Localizer[" PageErrorIntro " ]" Name =" Page" >
76+ <Button Icon =" fa-solid fa-font-awesome" Text =" @Localizer[" ButtonText " ]" OnClick =" OnGotoPage" />
77+ </DemoBlock >
78+
7579<AttributeTable Items =" @GetAttributes()" />
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ namespace BootstrapBlazor.Server.Components.Samples;
1010/// </summary>
1111public partial class GlobalException
1212{
13+ [ Inject ]
14+ [ NotNull ]
15+ private NavigationManager ? NavigationManager { get ; set ; }
16+
1317 [ Inject ]
1418 [ NotNull ]
1519 private SwalService ? SwalService { get ; set ; }
@@ -19,7 +23,6 @@ public partial class GlobalException
1923
2024 private static void OnClick ( )
2125 {
22- // NET6.0 采用 ErrorLogger 统一处理
2326 var a = 0 ;
2427 _ = 1 / a ;
2528 }
@@ -39,6 +42,12 @@ private Task OnShowDialog() => DialogService.Show(new DialogOption()
3942 Component = BootstrapDynamicComponent . CreateComponent < MockError > ( )
4043 } ) ;
4144
45+ private Task OnGotoPage ( )
46+ {
47+ NavigationManager . NavigateTo ( "/error-page" ) ;
48+ return Task . CompletedTask ;
49+ }
50+
4251 /// <summary>
4352 /// 获得属性方法
4453 /// </summary>
Original file line number Diff line number Diff line change 17251725 "Block2Intro": "Set custom exception handling logic by setting <code>OnErrorHandleAsync</code> callback method",
17261726 "DialogTitle": "In Dialog",
17271727 "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",
1728- "DialogText": "Popup"
1728+ "DialogText": "Popup",
1729+ "PageErrorTitle": "Page",
1730+ "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."
17291731 },
17301732 "BootstrapBlazor.Server.Components.Pages.GlobalOption": {
17311733 "Title": "Global exception",
Original file line number Diff line number Diff line change 17251725 "Block2Intro": "通过设置 <code>OnErrorHandleAsync</code> 回调方法,设置自定义异常处理逻辑",
17261726 "DialogTitle": "弹窗中异常捕获",
17271727 "DialogIntro": "点击按钮弹出弹窗,弹窗内按钮触发异常,错误显示在弹窗内",
1728- "DialogText": "弹窗"
1728+ "DialogText": "弹窗",
1729+ "PageErrorTitle": "页面异常捕获",
1730+ "PageErrorIntro": "点击按钮导航到页面生命周期内出错的页面,错误显示在当前页面,不影响菜单以及整体页面布局"
17291731 },
17301732 "BootstrapBlazor.Server.Components.Pages.GlobalOption": {
17311733 "Title": "全局配置",
Original file line number Diff line number Diff line change 11<Project Sdk =" Microsoft.NET.Sdk.Razor" >
22
33 <PropertyGroup >
4- <Version >9.7.0 </Version >
4+ <Version >9.7.1-beta01 </Version >
55 </PropertyGroup >
66
77 <ItemGroup >
Original file line number Diff line number Diff line change 66using Microsoft . AspNetCore . Components . Rendering ;
77using Microsoft . Extensions . Configuration ;
88using Microsoft . Extensions . Logging ;
9+ using System . Reflection ;
910
1011namespace BootstrapBlazor . Components ;
1112
@@ -71,10 +72,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
7172 var ex = CurrentException ?? _exception ;
7273 if ( ex != null )
7374 {
74- _exception = null ;
75-
7675 // 处理自定义异常逻辑
77- if ( OnErrorHandleAsync != null )
76+ if ( OnErrorHandleAsync != null )
7877 {
7978 // 页面生命周期内异常直接调用这里
8079 _ = OnErrorHandleAsync ( Logger , ex ) ;
@@ -83,6 +82,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
8382
8483 // 渲染异常内容
8584 builder . AddContent ( 0 , ExceptionContent ( ex ) ) ;
85+
86+ // 重置 CurrentException
87+ ResetException ( ) ;
8688 }
8789 else
8890 {
@@ -91,6 +93,16 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
9193 }
9294 }
9395
96+ private PropertyInfo ? _currentExceptionPropertyInfo ;
97+
98+ private void ResetException ( )
99+ {
100+ _exception = null ;
101+
102+ _currentExceptionPropertyInfo ??= GetType ( ) . BaseType ! . GetProperty ( nameof ( CurrentException ) , BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
103+ _currentExceptionPropertyInfo . SetValue ( this , null ) ;
104+ }
105+
94106 private Exception ? _exception = null ;
95107
96108 private RenderFragment < Exception > ExceptionContent => ex => builder =>
You can’t perform that action at this time.
0 commit comments