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

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

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

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

  现在我们按照上面的方式运行我们的程序,在分别输入错误的用户名和密码的情况下会自动显现相应的错误页面。

  四、自定义ActionInvoker:ExceptionActionInvoker

  对于上述的两种不同的异常处理方式最终是通过自定义的ActionInvoker来实现的,我们将其命名为ExceptionActionInvoker。如下面的代码片断所式,ExceptionActionInvoker直接继承自ControllerActionInvoker。属性ExceptionPolicy是一个基于指定的异常策略名称创建的ExceptionPolicyImpl 对象,用于针对EntLib进行的异常处理。而属性GetErrorView是一个用于获得作为错误页面的ViewResult对象的委托。整个异常处理的核心定义在InvokeAction方法中,该方法中指定的handleErrorActionName参数代表的是“异常处理操作名称”,整个方法就是按照上述的异常处理策略实现的。

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Web;  
  5. usingSystem.Web.Mvc;  
  6. usingArtech.Mvc.ExceptionHandling.Configuration;  
  7. usingMicrosoft.Practices.EnterpriseLibrary.Common.Configuration;  
  8. usingMicrosoft.Practices.EnterpriseLibrary.ExceptionHandling;  
  9. namespaceArtech.Mvc.ExceptionHandling  
  10. {  
  11. publicclassExceptionActionInvoker: ControllerActionInvoker  
  12. {  
  13. protectedExceptionHandlingSettings ExceptionHandlingSettings{get; privateset;}  
  14. protectedvirtualFunc<string, HandleErrorInfo, ViewResult> GetErrorView { get; privateset; }  
  15. publicExceptionPolicyImpl ExceptionPolicy { get; privateset; }  
  16. publicExceptionActionInvoker(stringexceptionPolicy,Func<string, HandleErrorInfo, ViewResult> getErrorView)  
  17. {  
  18. this.ExceptionPolicy = EnterpriseLibraryContainer.Current.GetInstance<ExceptionPolicyImpl>(exceptionPolicy);  
  19. this.GetErrorView = getErrorView;  
  20. this.ExceptionHandlingSettings = ExceptionHandlingSettings.GetSection();  
  21. }  
  22. publicoverrideboolInvokeAction(ControllerContext controllerContext, stringhandleErrorActionName)  
  23. {  
  24. ExceptionContext exceptionContext = controllerContext asExceptionContext;  
  25. if(null== exceptionContext)  
  26. {  
  27. thrownewArgumentException("The controllerContext must be ExceptionContext!""controllerContext");  
  28. }  
  29. try 
  30. {  
  31. exceptionContext.ExceptionHandled = true;  
  32. if(this.ExceptionPolicy.HandleException(exceptionContext.Exception))  
  33. {  
  34. HandleRethrownException(exceptionContext);  
  35. }  
  36. else 
  37. {  
  38. if(ExceptionHandlingContext.Current.Errors.Count == 0)  
  39. {  
  40. ExceptionHandlingContext.Current.Errors.Add(exceptionContext.Exception.Message);  
  41. }  
  42. ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(exceptionContext);  
  43. ActionDescriptor handleErrorAction = FindAction(exceptionContext, controllerDescriptor, handleErrorActionName);  
  44. if(null!= handleErrorAction)  
  45. {  
  46. IDictionary<stringobject>parameters = GetParameterValues(controllerContext, handleErrorAction);  
  47. exceptionContext.Result = this.InvokeActionMethod(exceptionContext, handleErrorAction, parameters);  
  48. }  
  49. else 
  50. {  
  51. HandleRethrownException(exceptionContext);  
  52. }  
  53. }  
  54. returntrue;  
  55. }  
  56. catch(Exception ex)  
  57. {  
  58. exceptionContext.Exception = ex;60:HandleRethrownException(exceptionContext);  
  59. returntrue;  
  60. }  
  61. }  
  62. protectedvirtualvoidHandleRethrownException(ExceptionContext exceptionContext)  
  63. {  
  64. stringerrorViewName = this.GetErrorViewName(exceptionContext.Exception.GetType());  
  65. stringcontrollerName = (string)exceptionContext.RouteData.GetRequiredString("controller");  
  66. stringaction = (string)exceptionContext.RouteData.GetRequiredString("action");  
  67. HandleErrorInfo handleErrorInfo = newHandleErrorInfo(exceptionContext.Exception, controllerName, action);70:exceptionContext.Result = this.GetErrorView(errorViewName, handleErrorInfo);  
  68. }  
  69. protectedstringGetErrorViewName(Type exceptionType)  
  70. {  
  71. ExceptionErrorViewElement element = ExceptionHandlingSettings.ExceptionErrorViews75:.Cast<ExceptionErrorViewElement>().FirstOrDefault(el=>el.ExceptionType == exceptionType);  
  72. if(null!= element)  
  73. {  
  74. returnelement.ErrorView;  
  75. }  
  76. if(null== element &&null!= exceptionType.BaseType!= null)  
  77. {  
  78. returnGetErrorViewName(exceptionType.BaseType);  
  79. }  
  80. else 
  81. {  
  82. return"Error";  
  83. }  
  84. }  
  85. }  
  86. }

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

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号