编程语言
首页 > 编程语言> > c#-流利的验证失败后如何调用方法

c#-流利的验证失败后如何调用方法

作者:互联网

如果流畅的验证方法失败,我想运行一个方法.

RuleFor(x => x.SheepName)
            .Must(x => x.SheepName == null)
            .When(x => x.HasSheep == false)
            .Otherwise(callMethod());

因此,在这种情况下,如果HasSheep值为false,但仍然给出SheepName,那么我想运行一个方法(在该示例中,该方法称为“ callMethod()”).

我已经编好了.Otherwise语句,所以寻找整行’.Otherwise(callMethod());’需要是..

解决方法:

您正在寻找记录为here的OnFailure(…)

You can make use of the OnAnyFailure and OnFailure (as of 8.0) callbacks to run a method if validation fails.

RuleFor(x => x.SheepName)
       .Must(x => x.SheepName == null)
       .When(x => x.HasSheep == false)
       .OnFailure(x => callMethod());

标签:validation,c,fluentvalidation
来源: https://codeday.me/bug/20191024/1922919.html