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: }