asp.net-mvc – 更改ASP.NET MVC 3中的默认ModelState错误消息

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 更改ASP.NET MVC 3中的默认ModelState错误消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将资源文件放在单独的程序集MyApp.Resources.dll中.我可以毫无问题地使用资源,但是当我想更改(本地化)默认验证消息时会出现问题:

“{0}字段是必需的.”和“值'{0}’对{1}无效.”

solution DefaultModelBinder.ResourceClassKey =“MyApp.Resources.Global”;不起作用,因为它要求ResourceClassKey位于Web项目中的App_GlobalResources文件夹下.

应该为我解决什么?

问候

解决方法

我找到了这种情况的解决方案(当资源处于单独的程序集中时).

为了使它工作,您应该创建自定义ResourceProviderFactory并将其注册为< globalization>中的默认ResourceProviderFactoryType. web.config部分.

设置本地化

// Modify web.config in run-time and setup custom ResourceProviderFactory
var globalization = WebConfigurationManager.GetSection("system.web/globalization") as GlobalizationSection;
var readonlyField = typeof(ConfigurationElement).GetField("_bReadOnly",BindingFlags.Instance | BindingFlags.NonPublic);
readonlyField.SetValue(globalization,false);
globalization.ResourceProviderFactoryType = typeof(ValidationResourceProviderFactory).FullName;

var resourcesClass = typeof(ValidationResources).FullName; 
DefaultModelBinder.ResourceClassKey = resourcesClass;
ValidationExtensions.ResourceClassKey = resourcesClass;

ValidationResourceProviderFactory

public sealed class ValidationResourceProviderFactory: System.Web.Compilation.ResourceProviderFactory
{
    public ValidationResourceProviderFactory()
    {
    }

    public override IResourceProvider CreateGlobalResourceProvider(string classKey)
    {
        return new GlobalResourceProvider(classKey);
    }

    public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
    {
        throw new NotImplementedException("Local resources are not supported yet");
    }
}

GlobalResourceProvider

public class GlobalResourceProvider : IResourceProvider
{
    public GlobalResourceProvider(string classKey)
    {
        Throw.IfBadArgument(() => String.IsNullOrEmpty(classKey),"classKey");

        var type = Type.GetType(classKey,false);
        if (type == null)
        {
            var asmName = classKey;
            var className = classKey;
            while(asmName.IndexOf(".") > -1 && type == null) 
            {
                asmName = asmName.Substring (0,asmName.LastIndexOf("."));
                className = classKey.Substring(asmName.Length + 1);
                type = Type.GetType(classKey + "," + asmName,false);
            }
        }

        Throw.IfNullArgument(type,"type");

        Manager = CreateResourceManager(classKey,type.Assembly);
    }

    public ResourceManager Manager { get; set; }

    #region IResourceProvider implementation

    public IResourceReader ResourceReader { get; set; }

    public object GetObject(string resourceKey,CultureInfo culture)
    {
        return Manager.GetObject(resourceKey,culture);
    }

    #endregion

    private ResourceManager CreateResourceManager(string classKey,Assembly assembly)
    {
        return new ResourceManager(classKey,assembly);
    }
}

UPD

RESidation for ValidationResources

只需将新资源类添加为ValidationResources并在其中放置提供的XML

<?xml version="1.0" encoding="utf-8"?>
<root>
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
   <!--  Schema definited removed -->
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader,System.Windows.Forms,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter,PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="Accept" xml:space="preserve">
    <value>Please enter a value with a valid mimetype.</value>
  </data>
  <data name="Creditcard" xml:space="preserve">
    <value>Please enter a valid credit card number.</value>
  </data>
  <data name="Date" xml:space="preserve">
    <value>Please enter a valid date.</value>
  </data>
  <data name="DateISO" xml:space="preserve">
    <value>Please enter a valid date (ISO).</value>
  </data>
  <data name="DateTime" xml:space="preserve">
    <value>Please enter a valid date and time.</value>
  </data>
  <data name="Digits" xml:space="preserve">
    <value>Please enter only digits.</value>
  </data>
  <data name="Email" xml:space="preserve">
    <value>Please enter a valid email address.</value>
  </data>
  <data name="EqualTo" xml:space="preserve">
    <value>Please enter the same value again.</value>
  </data>
  <data name="FieldMustBeDate" xml:space="preserve">
    <value>Please enter a valid date for "{0}".</value>
    <comment>Localization for legacy MVC ClientDataTypeModelValidatorProvider</comment>
  </data>
  <data name="FieldMustBeNumeric" xml:space="preserve">
    <value>Please enter a valid number for "{0}".</value>
    <comment>Localization for legacy MVC ClientDataTypeModelValidatorProvider</comment>
  </data>
  <data name="InvalidPropertyValue" xml:space="preserve">
    <value>Invalid property value: {0}</value>
  </data>
  <data name="Max" xml:space="preserve">
    <value>Please enter a value less than or equal to {0}.</value>
  </data>
  <data name="MaxLength" xml:space="preserve">
    <value>Please enter no more than {0} characters.</value>
  </data>
  <data name="Min" xml:space="preserve">
    <value>Please enter a value greater than or equal to {0}.</value>
  </data>
  <data name="MinLength" xml:space="preserve">
    <value>Please enter at least {0} characters.</value>
  </data>
  <data name="Number" xml:space="preserve">
    <value>Please enter a valid number.</value>
  </data>
  <data name="PropertyValueInvalid" xml:space="preserve">
    <value>The value "{0}" is invalid for the property "{1}"</value>
    <comment>Localization for legacy MVC DefaultModelBinder</comment>
  </data>
  <data name="PropertyValuerequired" xml:space="preserve">
    <value>The "{0}" field is required.</value>
    <comment>Localization for legacy MVC DefaultModelBinder</comment>
  </data>
  <data name="Range" xml:space="preserve">
    <value>Please enter a value between {1} and {2}.</value>
  </data>
  <data name="RangeClient" xml:space="preserve">
    <value>Please enter a value between {0} and {1}.</value>
  </data>
  <data name="RangeLength" xml:space="preserve">
    <value>Please enter a value between {0} and {1} characters long.</value>
  </data>
  <data name="Remote" xml:space="preserve">
    <value>Please fix this field.</value>
  </data>
  <data name="SignedInt" xml:space="preserve">
    <value>Please enter an integer value,sign allowed.</value>
  </data>
  <data name="Time" xml:space="preserve">
    <value>Please enter a valid time.</value>
  </data>
  <data name="UnsignedInt" xml:space="preserve">
    <value>Please enter a positive integer value.</value>
  </data>
  <data name="Url" xml:space="preserve">
    <value>Please enter a valid URL.</value>
  </data>
  <data name="ValueNull" xml:space="preserve">
    <value>&lt;null&gt;</value>
  </data>
</root>

猜你在找的asp.Net相关文章