1. 程式人生 > >本地化ASP.NET核心模型綁定錯誤消息

本地化ASP.NET核心模型綁定錯誤消息

ali rac cep ice inf 本地 模型 命名 選項

默認錯誤消息:

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核心模型綁定錯誤消息