Welcome 微信登录

首页 / 网页编程 / ASP.NET / ASP.NET MVC集成EntLib实现“自动化”异常处理[实现篇]

ASP.NET MVC集成EntLib实现“自动化”异常处理[实现篇]2012-08-30 博客园 Artech通过《》的实演示可以看出我们通过扩展实现的自动异常处理机制能够 利用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: }