我想捕获所有DbEntityValidationException消息,将它们添加到ModelState并继续到导致Exception的视图,该异常将使用@ Html.ValidationSummary()显示ModelState错误
除了HandleErrorAttribute的Extension类之外,我已经完成所有工作了我只写了重定向到/ Shared / Error视图 或者,如果我写 - filterContext.ExceptionHandled = true; 它返回一个空白视图
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?
}
}
}