- <exceptionHandling>
- <exceptionPolicies>
- <addnameaddname="myPolicy">
- <exceptionTypes>
- <addnameaddname="InvalidUserNameException"type="Artech.Mvc.ExceptionHandling.Models.InvalidUserNameException, Artech.Mvc.ExceptionHandling"
- postHandlingAction="ThrowNewException">
- ...
- <addnameaddname="UserNamePasswordNotMatchException"type="Artech.Mvc.ExceptionHandling.Models.UserNamePasswordNotMatchException, Artech.Mvc.ExceptionHandling"9:postHandlingAction="ThrowNewException">
- ...
- </add>
- </exceptionTypes>
- </add>
- </exceptionPolicies>
- </exceptionHandling>
按照我们上面的异常处理策略,在这种情况下我们将采用“错误页面”的方式来进行异常处理。也HandleErrorAttribute的处理方式类似,我们支持异常类型和Error View之间的匹配关系,而这是通过类似于如下的配置来定义的。值得一提的是,这里的异常类型是经过处理后重新抛出的异常
- <exceptionHandling>
- <exceptionPolicies>
- <addnameaddname="myPolicy">
- <exceptionTypes>
- <addnameaddname="InvalidUserNameException"type="Artech.Mvc.ExceptionHandling.Models.InvalidUserNameException, Artech.Mvc.ExceptionHandling"
- postHandlingAction="ThrowNewException">
- ...
- <addnameaddname="UserNamePasswordNotMatchException"type="Artech.Mvc.ExceptionHandling.Models.UserNamePasswordNotMatchException, Artech.Mvc.ExceptionHandling"9:postHandlingAction="ThrowNewException">
- ...
- </add>
- </exceptionTypes>
- </add>
- </exceptionPolicies>
- </exceptionHandling>
如上面的配置所示,我们为InvalidUserNameException和UserNamePasswordNotMatchException这两种异常类型定义了不同的Error View,分别是“InvalideUserNameError”和“UserNamePasswordNotMatchError”,详细定义如下所示:
- @{
- Layout = null;
- }
- <!DOCTYPEhtml>
- <html>
- <head>
- <title>Error</title>
- </head>
- <body>
- <pstylepstyle="color:Red; font-weight:bold">Sorry,the user name you specify does not exist!</p>
- </body>
- </html>
- @{
- Layout = null;
- }
- <!DOCTYPEhtml>
- <html>
- <head>
- <title>Error</title>
- </head>
- <body>
- <pstylepstyle="color:Red; font-weight:bold">Sorry, The password does not match the given user name!</p>
- </body>
- </html>
现在我们按照上面的方式运行我们的程序,在分别输