码迷,mamicode.com
首页 > Web开发 > 详细

本地化ASP.NET核心模型绑定错误消息

时间:2018-09-13 14:15:25      阅读:382      评论:0      收藏:0      [点我收藏+]

标签: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核心模型绑定错误消息

若要本地化ASP.NET Core模型绑定错误消息,请按照下列步骤操作:

  1. 创建资源文件 - 在解决方案的Resources文件夹下创建资源文件,并将文件命名为ModelBindingMessages.resx名称可以是其他任何名称,但我们将使用它来创建本地化程序。

  2. 添加资源键 - 打开资源文件并添加要用于本地化错误消息的键和值。我使用了键和值

  3. 配置选项 - 在ConfigureServices方法中,添加时Mvc,配置其选项以设置以下内容的消息访问者ModelBindingMessageProvider

  4.  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     });

     

  5. 还要在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 });

     


      

本地化ASP.NET核心模型绑定错误消息

标签:binding   rop   VID   resource   options   style   color   bin   provider   

原文地址:https://www.cnblogs.com/SpeakHero/p/9639676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!