網站首頁 編程語言 正文
一、異常過濾器
異常篩選器用于實現IExceptionFilter接口,并在ASP.NET MVC管道執行期間引發了未處理的異常時執行。異常篩選器可用于執行諸如日志記錄或顯示錯誤頁之類的任務。HandleErrorAttribute類是異常篩選器的一個示例。
先來看看HandleErrorAttribute類的定義:
#region 程序集 System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 // D:\Practice\MVC\自定義異常過濾器\MVCCuetomerExcepFilter\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll #endregion namespace System.Web.Mvc { // // 摘要: // 表示一個特性,該特性用于處理由操作方法引發的異常。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] public class HandleErrorAttribute : FilterAttribute, IExceptionFilter { // // 摘要: // 初始化 System.Web.Mvc.HandleErrorAttribute 類的新實例。 public HandleErrorAttribute(); // // 摘要: // 獲取或設置異常的類型。 // // 返回結果: // 異常的類型。 public Type ExceptionType { get; set; } // // 摘要: // 獲取或設置用于顯示異常信息的母版視圖。 // // 返回結果: // 母版視圖。 public string Master { get; set; } // // 摘要: // 獲取此特性的唯一標識符。 // // 返回結果: // 此特性的唯一標識符。 public override object TypeId { get; } // // 摘要: // 獲取或設置用于顯示異常信息的頁視圖。 // // 返回結果: // 頁視圖。 public string View { get; set; } // // 摘要: // 在發生異常時調用。 // // 參數: // filterContext: // 操作篩選器上下文。 // // 異常: // T:System.ArgumentNullException: // filterContext 參數為 null。 public virtual void OnException(ExceptionContext filterContext); } }
從代碼中可以看出HandleErrorAttribute繼承了IExceptionFilter接口,并且有一個虛方法,如果要自定義異常過濾器,只需要繼承HandleErrorAttribute類并重寫HandleErrorAttribute類里面的虛方法即可。
二、示例
1、創建異常類
新建一個ExceptionFilters類繼承自HandleErrorAttribute,并重寫OnException方法,代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVCCuetomerExcepFilter.Extension { ////// 異常過濾器 /// public class ExceptionFilters : HandleErrorAttribute { ////// 在異常發生時調用 /// /// public override void OnException(ExceptionContext filterContext) { // 判斷是否已經處理過異常 if(!filterContext.ExceptionHandled) { // 獲取出現異常的controller和action名稱,用于記錄 string strControllerName = filterContext.RouteData.Values["controller"].ToString(); string strActionName = filterContext.RouteData.Values["action"].ToString(); // 定義一個HandleErrorInfo,用于Error視圖展示異常信息 HandleErrorInfo info = new HandleErrorInfo(filterContext.Exception, strControllerName, strActionName); ViewResult result = new ViewResult { ViewName = this.View, // 定義ViewData,泛型 ViewData = new ViewDataDictionary(info) }; // 設置操作結果 filterContext.Result = result; // 設置已經處理過異常 filterContext.ExceptionHandled = true; } //base.OnException(filterContext); } } }
2、創建控制器
新建一個控制器,代碼如下:
using MVCCuetomerExcepFilter.Extension; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVCCuetomerExcepFilter.Controllers { public class ExceptionController : Controller { // GET: Exception ////// View表示發生異常時指定的視圖 /// 這里表示發生異常時使用ExceptionDetails視圖 /// ///[ExceptionFilters(View =("ExceptionDetails"))] public ActionResult Index() { // 測試拋出異常 throw new NullReferenceException("測試拋出的異常"); } } }
異常發生時使用ExceptionDetails視圖,所以在Shared文件夾里面新建ExceptionDetails視圖,代碼如下:
@model System.Web.Mvc.HandleErrorInfo @{ Layout = null; }異常 拋錯控制器:@Model.ControllerName 拋錯方法:@Model.ActionName 拋錯類型: @Model.Exception.GetType().Name
異常信息:@Model.Exception.Message
堆棧信息:
@Model.Exception.StackTrace
三、測試
運行程序,訪問Exception控制器的Index方法,效果如下:
四、總結
上面的案例演示了一個自定義異常類,很明顯比HandleError要靈活,在自定義異常類里面可以寫很多與業務相關的代碼。
GitHub代碼地址:https://github.com/jxl1024/CustomerHandleErrorFilter
原文鏈接:https://www.cnblogs.com/dotnet261010/p/10848078.html
相關推薦
- 2022-11-17 C++11中異常處理機制詳解_C 語言
- 2023-03-22 Redis慢查詢日志及慢查詢分析詳解_Redis
- 2022-10-07 詳解Python?OpenCV圖像分割算法的實現_python
- 2022-04-12 C語言三分鐘精通時間復雜度與空間復雜度_C 語言
- 2021-11-26 linux服務器磁盤空間擴充方法_Linux
- 2023-02-17 react生命周期(類組件/函數組件)操作代碼_React
- 2022-10-04 python中numpy矩陣的零填充的示例代碼_python
- 2022-08-18 Android新建水平節點進度條示例_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支