2016-04-04 6 views
1

Wir verwenden das CQRS-Muster und wir haben ein Problem mit der Fehlerbehandlung der fließenden Validierung. (CQRS Muster @https://lostechies.com/jimmybogard/2015/05/05/cqrs-with-mediatr-and-automapper/)ValidationException von Fluent ValidationException innerhalb von MVC

public class OtherSpecified : AbstractValidator<Command> 
{ 
    public OtherSpecified(ApplicationDbContext context) 
    { 
     RuleFor(x => x.Other).NotNull(); 
    } 
} 

public class DepartmentSpecified : AbstractValidator<Command> 
{ 
    public DepartmentSpecified(ApplicationDbContext context) 
    { 
     RuleFor(x => x.Department).NotNull(); 
    } 
} 

Jetzt injizieren wir unsere Validierungshandler so können wir mehr abstrakten Validator basierend auf Informationen von http://lostechies.com/jimmybogard/2014/09/09/tackling-cross-cutting-concerns-with-a-mediator-pipeline/ laufen. Das funktioniert aber, weil ich die Regeln laufen in der foreach-Schleife sehen

public TResponse Handle(TRequest request) 
{ 
    var context = new ValidationContext(request); 
    var result = new ValidationResult(); 

    var list = _validators.ToList(); 
    foreach (var validator in list) 
    { 
     var results = validator.Validate(request); 
     foreach (var validationFailure in results.Errors) 
     { 
      result.Errors.Add(validationFailure); 
      //temp testing code below 
      if (results.Errors.Count > 0) 
       throw new ValidationException(result.Errors); 

     } 
    } 

    if (result.Errors != null && 
     result.Errors.Count > 0) 
    { 
     throw new ValidationException(result.Errors); 
    } 
    return _inner.Handle(request); 
} 

Das Problem ist, dass die fließend Validierung Ausnahme (Validation) behandelt wird nicht. Die Regel Fehler Blasen als

Beschreibung: Eine nicht behandelte Ausnahme trat während der Ausführung der der aktuellen Webanforderung auf. Bitte überprüfen Sie die Stack-Trace für weitere Informationen über den Fehler und wo es im Code entstanden ist.

Ausnahmedetails: FluentValidation.ValidationException: Validierung fehlgeschlagen:

Was würde ich erwarten, Fluent Validierung ist den Fehler zu behandeln und es zurück in die Ajax-Request in json als Validierungsfehler

+0

Ihre Frage nicht sehr spezifisch ist. Ich verstehe Ihr Problem, aber die Tatsache ist fließend nicht automatisch Validierung Ausnahmen behandelt, müssen Sie. Sie können benutzerdefinierten Code schreiben, um das zu tun, was Sie beschreiben, aber es wird nicht automatisch gemacht. – nhouser9

Antwort

3

gebe ich Fangen Sie die ValidationException mit einem HandleErrorAttribute ab und geben Sie json an die Ajax-Anforderung zurück.

public class ValidationExceptionHandlerErrorAttribute : HandleErrorAttribute 
    { 
     public override void OnException(ExceptionContext filterContext) 
     { 
      //only handle ValidationExceptions 
      if ((filterContext.Exception as ValidationException) != null) 
      { 
       var result = new ContentResult(); 
       //Add errors to Model State so they are handled auto-magically 
       foreach (var validationsfailures in (filterContext.Exception as ValidationException).Errors) 
       { 
        filterContext.Controller.ViewData.ModelState.AddModelError(validationsfailures.PropertyName,validationsfailures.ErrorMessage); 
       } 
       //convert to json and return to ajax request 
       string content = JsonConvert.SerializeObject(filterContext.Controller.ViewData.ModelState, 
        new JsonSerializerSettings 
        { 
         ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
        }); 
       result.Content = content; 
       result.ContentType = "application/json"; 
       filterContext.HttpContext.Response.StatusCode = 400; 
       filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
       filterContext.Result = result; 
       filterContext.ExceptionHandled = true; 
      } 
     } 
    } 

und ich verdrahten hier oben

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     .. 
     .. 
     filters.Add(new ValidationExceptionHandlerErrorAttribute()); 
    } 
Verwandte Themen