关闭

ASP.NET MVC基于异常处理的解决方案

发表于:2012-1-13 09:55

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Artech    来源:51Testing软件测试网采编

  五、自定义Controller:BaseController

  ExceptionActionInvoker最终在我们自定义的Controller基类BaseController中被调用的。ExceptionActionInvoker对象在构造函数中被初始化,并在重写的OnException方法中被调用。

  1. usingSystem;  
  2. usingSystem.Web.Mvc;  
  3. namespaceArtech.Mvc.ExceptionHandling  
  4. {  
  5. publicabstractclassBaseController : Controller  
  6. {  
  7. publicBaseController(stringexceptionPolicy)  
  8. {  
  9. Func<string, HandleErrorInfo, ViewResult> getErrorView = (viewName, handleErrorInfo) => this.View(viewName, handleErrorInfo);  
  10. this.ExceptionActionInvoker = newExceptionActionInvoker(exceptionPolicy,getErrorView);  
  11. }  
  12. publicBaseController(ExceptionActionInvoker actionInvoker)  
  13. {  
  14. this.ExceptionActionInvoker = actionInvoker;
  15. }
  16. publicvirtualExceptionActionInvoker ExceptionActionInvoker { get; privateset; }  
  17. protectedvirtualstringGetHandleErrorActionName(stringactionName)  
  18. {  
  19. returnstring.Format("On{0}Error", actionName);  
  20. }  
  21. protectedoverridevoidOnException(ExceptionContext filterContext)  
  22. {  
  23. using(ExceptionHandlingContextScope contextScope = newExceptionHandlingContextScope(filterContext))  
  24. {  
  25. stringactionName = RouteData.GetRequiredString("action");  
  26. stringhandleErrorActionName = this.GetHandleErrorActionName(actionName);  
  27. this.ExceptionActionInvoker.InvokeAction(filterContext, handleErrorActionName);  
  28. foreach(var error inExceptionHandlingContext.Current.Errors)  
  29. {  
  30. ModelState.AddModelError(Guid.NewGuid().ToString() ,error.ErrorMessage);  
  31. }  
  32. }  
  33. }  
  34. }  
  35. }

  值得一提的是:整个OnException方法中的操作都在一个ExceptionHandlingContextScope中进行的。顾名思义, 我们通过ExceptionHandlingContextScope为ExceptionHandlingContext创建了一个范围。ExceptionHandlingContext定义如下,我们可以通过它获得当前的ExceptionContext和ModelErrorCollection,而静态属性Current返回当前的ExceptionHandlingContext对象。

  1. publicclassExceptionHandlingContext  
  2. {  
  3. [ThreadStatic]  
  4. privatestaticExceptionHandlingContext current;  
  5. publicExceptionContext ExceptionContext { get; privateset; }  
  6. publicModelErrorCollection Errors { get; privateset; }  
  7. publicExceptionHandlingContext(ExceptionContext exceptionContext)  
  8. {  
  9. this.ExceptionContext = exceptionContext;
  10. this.Errors = newModelErrorCollection();  
  11. }  
  12. publicstaticExceptionHandlingContext Current  
  13. {  
  14. get { returncurrent; }  
  15. set { current = value;}  
  16. }  
  17. }

  在BaseController的OnException方法中,当执行了ExceptionActionInvoker的InvokeAction之后,我们会将当前ExceptionHandlingContext的ModelError转移到当前的ModelState中。这就是为什么我们会通过ValidationSummary显示错误信息的原因。对于我们的例子来说,错误消息的指定是通过如下所示的ErrorMessageSettingHandler 实现的,而它仅仅将指定的错误消息添加到当前ExceptionHandlingContext的Errors属性集合中而已。

  1. [ConfigurationElementType(typeof(ErrorMessageSettingHandlerData))]  
  2. publicclassErrorMessageSettingHandler : IExceptionHandler  
  3. {  
  4. publicstringErrorMessage { get; privateset; }  
  5. publicErrorMessageSettingHandler(stringerrorMessage)  
  6. {  
  7. this.ErrorMessage = errorMessage;  
  8. }  
  9. publicException HandleException(Exception exception, Guid handlingInstanceId)  
  10. {  
  11. if(null== ExceptionHandlingContext.Current)  
  12. {  
  13. thrownewInvalidOperationException("...");  
  14. }  
  15. if(string.IsNullOrEmpty(this.ErrorMessage))  
  16. {  
  17. ExceptionHandlingContext.Current.Errors.Add(exception.Message);  
  18. }  
  19. else 
  20. {  
  21. ExceptionHandlingContext.Current.Errors.Add(this.ErrorMessage);  
  22. }  
  23. returnexception;  
  24. }  
  25. }

55/5<12345
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号