我想捕获所有 DbEntityValidationException 消息,将它们添加到 ModelState 并继续查看导致异常的视图,该异常将使用 @Html.ValidationSummary() 显示 ModelState 错误
除了 HandleErrorAttribute 的 Extension 类之外,我已经完成了所有工作 它返回一个空白视图
public class HandleLogErrorAttribute : HandleErrorAttribute
{
//Log the filterContext Exception Details
public override void OnException ( ExceptionContext filterContext )
{
var exception = filterContext.Exception;
var controller = ( (Controller) filterContext.Controller );
//Log here
//Add Validation Exception messages to ModelState here
if ( exception is DbEntityValidationException ) {
var dbEx = filterContext.Exception as DbEntityValidationException;
foreach ( var ves in dbEx.EntityValidationErrors ){
foreach ( var ve in ves.ValidationErrors ) {
controller.ModelState.AddModelError( string.Empty, ve.ErrorMessage );
}
}
//HOW to continue to the View that caused the exception with the model state now filed with the Validation Exceptions?
}
}
}