ASP.NET Core Model Binding Error Messages Localization in ASP.NET CORE 2.0

前提是你 提交于 2019-12-10 19:29:32

问题


In ASP.NET CORE 1.1 it was possible to localize model binding error messages using a resource file and configure its options to set message accessors for ModelBindingMessageProvider in the Startup.cs like

services.AddMvc(options =>
{
    var F = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
    var L = F.Create("ModelBindingMessages", null);
    options.ModelBindingMessageProvider.ValueIsInvalidAccessor =
        (x) => L["The value '{0}' is invalid."];

as shown here: ASP.NET Core Model Binding Error Messages Localization and here: https://blogs.msdn.microsoft.com/mvpawardprogram/2017/05/09/aspnetcore-mvc-error-message/

In ASP.NET CORE 2.0 I receive an error message on all of the ModelBindingMessageProvider's properties

options.ModelBindingMessageProvider.ValueIsInvalidAccessor 

is readonly

How can these messages be localized in ASP.NET CORE 2.0


回答1:


I also ran into this. These setters were replaced with methods such as SetValueIsInvalidAccessor, the change is described here: https://github.com/aspnet/Announcements/issues/240




回答2:


In ASP.NET Core 2.0, model binding message provider properties have got read only, but a setter method for each property has been added.

So if you follow the example of my linked post, to set ValueIsInvalidAccessor, you should use SetValueIsInvalidAccessor method this way:

options.ModelBindingMessageProvider.SetValueIsInvalidAccessor (
    (x) => L["The value '{0}' is invalid."]);

Sample Project

I added the sample project for ASP.NET CORE 2.0 in the following repository:

  • r-aghaei/AspNetCoreLocalizationSample

Default Error Messages:

  • MissingBindRequiredValueAccessor: A value for the '{0}' property was not provided.
  • MissingKeyOrValueAccessor: A value is required.
  • MissingRequestBodyRequiredValueAccessor: A non-empty request body is required.
  • ValueMustNotBeNullAccessor: The value '{0}' is invalid.
  • AttemptedValueIsInvalidAccessor: The value '{0}' is not valid for {1}.
  • NonPropertyAttemptedValueIsInvalidAccessor: The value '{0}' is not valid.
  • UnknownValueIsInvalidAccessor: The supplied value is invalid for {0}.
  • NonPropertyUnknownValueIsInvalidAccessor: The supplied value is invalid.
  • ValueIsInvalidAccessor: The value '{0}' is invalid.
  • ValueMustBeANumberAccessor: The field {0} must be a number.
  • NonPropertyValueMustBeANumberAccessor: The field must be a number.



回答3:


you could use this configuration for localize mvc core error messages :

public class SomeMvcOptionsSetup : Microsoft.Extensions.Options.IConfigureOptions<Microsoft.AspNetCore.Mvc.MvcOptions>
{
    private readonly Microsoft.Extensions.Localization.IStringLocalizer _resourceLocalizer;

    public SomeMvcOptionsSetup()
    {
    }

    public SomeMvcOptionsSetup(Microsoft.Extensions.Localization.IStringLocalizerFactory stringLocalizerFactory)
    {
        _resourceLocalizer = stringLocalizerFactory.Create(baseName:"ResourceClassName",location:"ResourceNameSpace");
    }

    public void Configure(Microsoft.AspNetCore.Mvc.MvcOptions options)
    {
        options.ModelBindingMessageProvider.SetValueIsInvalidAccessor((x) =>
        {
            if (_resourceLocalizer == null)
            {
                return "Custom Error Message";
            }

            return _resourceLocalizer["Specific Resource Key In Resource File"]; 
        });

        options.ModelBindingMessageProvider.SetValueMustNotBeNullAccessor((x) =>
        {
            if (_resourceLocalizer == null)
            {
                return "Value Can not be null....";
            }
            return _resourceLocalizer["ResourceKeyValueCanNotBeNull"];
        });

  .
  .
  .


    }
}

Then, add the following to your Startup.ConfigureServices(...) method:

 services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>,SomeMvcOptionsSetup >());

Please see this link



来源:https://stackoverflow.com/questions/45927545/asp-net-core-model-binding-error-messages-localization-in-asp-net-core-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!