public void OnActionExecuting(ActionExecutingContext context) { if (context.ModelState.IsValid) { return; } var errors = context.ModelState .Where(x => x.Value != null) .ToDictionary(s => s.Key, s => string.Join("|", s.Value.Errors.Where(x => !string.IsNullOrEmpty(x.ErrorMessage)).Select(c => c.ErrorMessage))); var exceptionModel = new ExceptionModel() { Type = ExceptionModel.ExceptionType.Validation.ToString(), Key = "model_is_invalid", Errors = errors }; context.Result = new BadRequestObjectResult(exceptionModel); }
public void OnActionExecuted(ActionExecutedContext actionExecutedContext) { if (actionExecutedContext.Exception == null) { return; } var exceptionModel = new ExceptionModel() { Type = ExceptionModel.ExceptionType.System.ToString(), }; var exception = actionExecutedContext.Exception; if (exception is InvalidModelException) { exceptionModel.Key = "model_is_invalid"; } else { exceptionModel.Key = "error"; exceptionModel.Errors = new Dictionary <string, string>() { { actionExecutedContext.Exception.Source, actionExecutedContext.Exception.Message } }; } if (actionExecutedContext.Exception.Data != null) { exceptionModel.Errors = (Dictionary <string, string>)actionExecutedContext.Exception.Data; } actionExecutedContext.ExceptionHandled = true; actionExecutedContext.Result = new BadRequestObjectResult(exceptionModel); }