如何本地化.NET API中的“X字段是必需的”本地错误?

7eumitmz  于 2023-05-30  发布在  .NET
关注(0)|答案(2)|浏览(128)

bounty将在6天内到期。此问题的答案有资格获得+50声望奖励。ervin正在寻找一个答案从一个有信誉的来源

我正在通过本地化API构建自定义验证响应。我需要在请求API时本地化模型绑定上的变量的默认错误消息。我本地化了几乎所有的东西,但我仍然得到了英语的The X field is required
我使用了this response的解决方案,它与.NET 7配合良好,但是此选项配置器错过了The X field is required的消息。
我还找到了添加[Required]属性并覆盖其消息的解决方案,但应用程序处于高级状态,因此这将花费大量时间。

qoefvg9y

qoefvg9y1#

为什么不用fluentvalidation docs.fluentvalidation.net/en/latest/aspnet.html

wecizke3

wecizke32#

使用FluentValidation时,您可以按如下方式定义规则:RuleFor(v => v.Item) .NotNull() .WithMessage("'Item' cannot be null.");
在WithMessage()中提供本地化。

相关问题