【发布时间】:2022-06-13 14:06:53
【问题描述】:
我有错误模型,例如
并在我的验证器中进行规则
RuleFor(d => d.Name).NotEmpty().WithMessage("{PropertyName} is required.")
有没有办法添加新的自定义选项,例如
RuleFor(d => d.Name).NotEmpty().WithMessage("{PropertyName} is required.").**WithCustomErrorMessage("Example message content")**?
编辑: 我想从我的 web api 应用程序返回错误列表。 每个错误看起来都像这样:
public class Error
{
public string ErrorName{ get; set; }
public string ErrorDetails{ get; set; }
public string ErrorCode{ get; set; }
public string FieldPath{ get; set; }
}
我想在验证器中分配这些值,然后使用 ValidationResult 对象在我的处理程序中创建一个错误列表。
感谢您的建议。
【问题讨论】:
-
这个的用例是什么?为什么需要常规错误消息和自定义错误消息?
-
我刚刚编辑了我的帖子 :)
-
为你编辑了我的答案
标签: c# .net-core fluentvalidation