相关疑难解决方法(0)

FluentValidation:如何将所有验证消息放在一个位置?

这是我的验证类之一:

public class StocksValidator : AbstractValidator<Stocks>
    {
        public StocksValidator()
        {
            RuleFor(x => x.SellerId).GreaterThan(1).WithMessage("SellerId should be greater than 1")
                                    .LessThan(100).WithMessage("SellerId should be less than 100");
            RuleFor(x => x.SellerType).GreaterThan(101).WithMessage("SellerType should be greater than 101")
                                    .LessThan(200).WithMessage("SellerType should be less than 200");
            RuleFor(x => x.SourceId).GreaterThan(201).WithMessage("SourceId should be greater than 201")
                                    .LessThan(300).WithMessage("SourceId should be less than 300");
        }
    }
Run Code Online (Sandbox Code Playgroud)

我知道这些消息,例如{field}应该少于{x}应该位于公共位置,而不是这里。但是我不知道如何集中他们?

  1. 一种方法是使用所有这些常量字符串创建新的c#文件。这很简单。

  2. 在Web API中使用本地化和流畅的验证。这有什么好处。我在哪里可以找到好的教程?

c# asp.net localization fluentvalidation

6
推荐指数
1
解决办法
2751
查看次数

标签 统计

asp.net ×1

c# ×1

fluentvalidation ×1

localization ×1