流畅验证失败后如何调用方法

How to call a method after fluent validation faliure

如果流式验证方法失败,我想运行一个方法。

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

所以在这种情况下,如果 HasSheep 值为 false 但 SheepName 仍然给出,那么我想 运行 一个方法(在示例中该方法被称为 'callMethod()')。

我已经编写了 .Otherwise 语句,所以要查找整行 '.Otherwise(callMethod());' 的内容需要..

您正在寻找 OnFailure(…),已记录 here

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());