ASP.NET MVC整合EntLib實現“自動化”異常處理[實現篇]

來源:互聯網
上載者:User

通過《執行個體篇》的實示範可以看出我們通過擴充實現的自動異常處理機制能夠 利用EntLib的EHAB根據執行的一場處理策略對某個Action方法執行過程中拋出的 異常進行處理。對於處理後的結果,則按照如下的機制對請求進行響應。[原始碼 從這裡下載]

對於Ajax請求,直接建立一個用於封裝被處理後異常的資料對象,並據此建立 一個JsonResult將異常資訊回複給用戶端。

對於非Ajax請求,如果當前Action方法上應用HandleErrorActionAttribute特 性設定了匹配的Action方法用於處理該方法拋出的異常,那麼執行該方法並用返 回的ActionResult對象響應當前請求。

如果HandleErrorActionAttribute特性不曾應用在當前Action方法上,或者通 過該特性指定的Action不存在,則將預設的錯誤View呈現出來作為多請求的響應 。

一、ExceptionPolicyAttribute & HandleErrorActionAttribute

所有的這些都是通過一個自訂的ExceptionFilter來實現的。不過我們並沒 有定義任何的ExceptionFilter特性,而是將異常處理實現在一個自訂的 ExtendedController基類中,對異常的自動處理實現在重寫的OnException方法中 ,不過在介紹該方法的邏輯之前我們先來看看定義在ExtendedController中的其 他輔助成員。

   1: public class ExtendedController: Controller
2: {
3: private static Dictionary<Type, ControllerDescriptor> controllerDescriptors = new Dictionary<Type, ControllerDescriptor>();
4: private static object syncHelper = new object();
5:
6: protected override void OnException(ExceptionContext filterContext)
7: {
8: //省略成員
9: }
10:
11: //描述當前Controller的ControllerDescriptor
12: public ControllerDescriptor Descriptor
13: {
14: get
15: {
16: ControllerDescriptor descriptor;
17: if(controllerDescriptors.TryGetValue(this.GetType(), out descriptor))
18: {
19: return descriptor;
20: }
21: lock (syncHelper)
22: {
23: if (controllerDescriptors.TryGetValue(this.GetType(), out descriptor))
24: {
25: return descriptor;
26: }
27: else
28: {
29: descriptor = new ReflectedControllerDescriptor(this.GetType());
30: controllerDescriptors.Add(this.GetType(), descriptor);
31: return descriptor;
32: }
33: }
34: }
35: }
36: //擷取異常處理策略名稱
37: public string GetExceptionPolicyName()
38: {
39: string actionName = ControllerContext.RouteData.GetRequiredString("action");
40: ActionDescriptor actionDescriptor = this.Descriptor.FindAction(ControllerContext, actionName);
41: if (null == actionDescriptor)
42: {
43: return string.Empty;
44: }
45: ExceptionPolicyAttribute exceptionPolicyAttribute = actionDescriptor.GetCustomAttributes(true).OfType<ExceptionPolicyAttribute>().FirstOrDefault()??
46: Descriptor.GetCustomAttributes(true).OfType<ExceptionPolicyAttribute>().FirstOrDefault()?? new ExceptionPolicyAttribute("");
47: return exceptionPolicyAttribute.ExceptionPolicyName;
48: }
49:
50: //擷取Handle-Error-Action名稱
51: public string GetHandleErrorActionName()
52: {
53: string actionName = ControllerContext.RouteData.GetRequiredString("action");
54: ActionDescriptor actionDescriptor = this.Descriptor.FindAction(ControllerContext, actionName);
55: if (null == actionDescriptor)
56: {
57: return string.Empty;
58: }
59: HandleErrorActionAttribute handleErrorActionAttribute = actionDescriptor.GetCustomAttributes(true).OfType<HandleErrorActionAttribute>().FirstOrDefault()??
60: Descriptor.GetCustomAttributes(true).OfType<HandleErrorActionAttribute>().FirstOrDefault()?? new HandleErrorActionAttribute("");
61: return handleErrorActionAttribute.HandleErrorAction;
62: }
63:
64: //用於執行Handle-Error-Action的ActionInvoker
65: public HandleErrorActionInvoker HandleErrorActionInvoker { get; private set; }
66:
67: public ExtendedController()
68: {
69: this.HandleErrorActionInvoker = new HandleErrorActionInvoker();
70: }
71: }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.