498)this.width=498;'' onmousewheel = ''javascript:return big(this)'' alt="" src="http://images.myeducs.cn/files/uploadimg/20120111/1056421.png" />
四、自定义ActionInvoker:ExceptionActionInvoker
对于上述的两种不同的异常处理方式最终是通过自定义的ActionInvoker来实现的,我们将其命名为ExceptionActionInvoker。如下面的代码片断所式,ExceptionActionInvoker直接继承自ControllerActionInvoker。属性ExceptionPolicy是一个基于指定的异常策略名称创建的ExceptionPolicyImpl 对象,用于针对EntLib进行的异常处理。而属性GetErrorView是一个用于获得作为错误页面的ViewResult对象的委托。整个异常处理的核心定义在InvokeAction方法中,该方法中指定的handleErrorActionName参数代表的是“异常处理操作名称”,整个方法就是按照上述的异常处理策略实现的。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Mvc;
- usingArtech.Mvc.ExceptionHandling.Configuration;
- usingMicrosoft.Practices.EnterpriseLibrary.Common.Configuration;
- usingMicrosoft.Practices.EnterpriseLibrary.ExceptionHandling;
- namespaceArtech.Mvc.ExceptionHandling
- {
- publicclassExceptionActionInvoker: ControllerActionInvoker
- {
- protectedExceptionHandlingSettings ExceptionHandlingSettings{get; privateset;}
- protectedvirtualFunc<string, HandleErrorInfo, ViewResult> GetErrorView { get; privateset; }
- publicExceptionPolicyImpl ExceptionPolicy { get; privateset; }
- publicExceptionActionInvoker(stringexceptionPolicy,Func<string, HandleErrorInfo, ViewResult> getErrorView)
- {
- this.ExceptionPolicy = EnterpriseLibraryContainer.Current.GetInstance<ExceptionPolicyImpl>(exceptionPolicy);
- this.GetErrorView = getErrorView;
- this.ExceptionHandlingSettings = ExceptionHandlingSettings.GetSection();
- }
- publicoverrideboolInvokeAction(ControllerContext controllerContext, stringhandleErrorActionName)
- {
- ExceptionContext exceptionContext = controllerContext asExceptionContext;
- if(null== exceptionContext)
- {
- thrownewArgumentException("The controllerContext must be ExceptionContext!", "controllerContext");
- }
- try
- {
- exceptionContext.ExceptionHandled = true;
- if(this.ExceptionPolicy.HandleException(exceptionContext.Exception))
- {
- HandleRethrownException(exceptionContext);
- }
- else
- {
- if(ExceptionHandlingContext.Current.Errors.Count == 0)
- {
- ExceptionHandlingContext.Current.Errors.Add(exceptionContext.Exception.Message);
- }
- ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(exceptionContext);
- ActionDescriptor handleErrorAction = FindAction(exceptionContext, controllerDescriptor, handleErrorActionName);
- if(null!= handleErrorAction)
- {
- IDictionary<string, object>parameters = GetParameterValues(controllerContext, handleErrorAction);
- exceptionContext.Result = this.InvokeActionMethod(exceptionContext, handleErrorAction, parameters);
- }
- else
- {
- HandleRethrownException(exceptionContext);
- }
- }
- returntrue;
- }
- catch(Exception ex)
- {
- exceptionContext.Exception = ex;60:HandleRethrownException(exceptionContext);
- returntrue;
- }
- }
- protectedvirtualvoidHandleRethrownException(ExceptionContext exceptionContext)
- {
- stringerrorViewName = this.GetErrorViewName(exceptionContext.Exception.GetType());
- stringcontrollerName = (string)exceptionContext.RouteData.GetRequiredString("controller");
- stringaction = (string)exceptionContext.RouteData.GetRequiredString("action");
- HandleErrorInfo handleErrorInfo = newHandleErrorInfo(exceptionContext.Exception, controllerName, action);70:exceptionContext.Result = this.GetErrorView(errorViewName, handleErrorInfo);
- }
- protectedstringGetErrorViewName(Type exceptionType)
- {
- ExceptionErrorViewElement element = ExceptionHandlingSettings.ExceptionErrorViews75:.Cast<ExceptionErrorViewElement>().FirstOrDefault(el=>el.ExceptionType == exceptionType);
- if(null!= element)
- {
- returnelement.ErrorView;
- }
- if(null== element &&null!= exceptionType.BaseType!= null)
- {
- returnGetErrorViewName(exceptionType.BaseType);
- }
- else
- {
- return"Error";
- }
- }
- }
- }
五、自定义Controller:BaseController
E