标签:binding rop VID resource options style color bin provider
MissingBindRequiredValueAccessor A value for the ‘{0}‘ property was not provided.
MissingKeyOrValueAccessor A value is required.
ValueMustNotBeNullAccessor The value ‘{0}‘ is invalid.
AttemptedValueIsInvalidAccessor The value ‘{0}‘ is not valid for {1}.
UnknownValueIsInvalidAccessor The supplied value is invalid for {0}.
ValueIsInvalidAccessor The value ‘{0}‘ is invalid.
ValueMustBeANumberAccessor The field {0} must be a number.
MissingRequestBodyRequiredValueAccessor A non-empty request body is required.
NonPropertyAttemptedValueIsInvalidAccessor The value ‘{0}‘ is not valid.
NonPropertyUnknownValueIsInvalidAccessor The supplied value is invalid.
NonPropertyValueMustBeANumberAccessor The field must be a number.
若要本地化ASP.NET Core模型绑定错误消息,请按照下列步骤操作:
创建资源文件 - 在解决方案的Resources文件夹下创建资源文件,并将文件命名为ModelBindingMessages.resx。名称可以是其他任何名称,但我们将使用它来创建本地化程序。
添加资源键 - 打开资源文件并添加要用于本地化错误消息的键和值。我使用了键和值
配置选项 - 在ConfigureServices
方法中,添加时Mvc
,配置其选项以设置以下内容的消息访问者ModelBindingMessageProvider
1 services.AddMvc(options => 2 { 3 IStringLocalizerFactory F = services.BuildServiceProvider(). 4 GetService<IStringLocalizerFactory>(); 5 IStringLocalizer L = F.Create("ModelBindingMessages", 6 "AspNetCoreLocalizationSample"); 7 options.ModelBindingMessageProvider. 8 SetValueIsInvalidAccessor((x) => L["The value ‘{0}‘ is invalid."]); 9 options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor( 10 (x) =>L["The field {0} must be a number."]); 11 options.ModelBindingMessageProvider.SetMissingBindRequiredValueAccessor( 12 (x) => L["A value for the ‘{0}‘ property was not provided.", x]); 13 options.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor( 14 (x, y) => L["The value ‘{0}‘ is not valid for {1}.", x, y]); 15 options.ModelBindingMessageProvider.SetMissingKeyOrValueAccessor( 16 () => L["A value is required."]); 17 options.ModelBindingMessageProvider.SetUnknownValueIsInvalidAccessor( 18 (x) => L["The supplied value is invalid for {0}.", x]); 19 options.ModelBindingMessageProvider.SetValueMustNotBeNullAccessor( 20 (x) => L["Null value is invalid.", x]); 21 }).AddDataAnnotationsLocalization() 22 .AddViewLocalization(); 23 services.Configure<RequestLocalizationOptions>(options => 24 { 25 var supportedCultures = new[]{new CultureInfo("en"), new CultureInfo("zh-cn")}; 26 options.DefaultRequestCulture = new RequestCulture("en", "en"); 27 options.SupportedCultures = supportedCultures; 28 options.SupportedUICultures = supportedCultures; 29 });
还要在Configure
方法开头添加此代码:
1 var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("zh-CN") }; 2 app.UseRequestLocalization(new RequestLocalizationOptions() 3 { 4 DefaultRequestCulture = new RequestCulture(new CultureInfo("en")), 5 SupportedCultures = supportedCultures, 6 SupportedUICultures = supportedCultures 7 });
标签:binding rop VID resource options style color bin provider
原文地址:https://www.cnblogs.com/SpeakHero/p/9639676.html