问题描述
不显眼的客户端验证规则中的验证类型名称必须是独特的.以下验证类型已多次出现:必填
这里指的是 EmailAddress 属性,这里:
This is referring to the EmailAddress property, here:
public class LoginModel
{
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
[AdditionalMetadata("Style", "Wide")]
public string EmailAddress { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[AdditionalMetadata("Style", "Wide")]
public string Password { get; set; }
}
我没有在这里两次使用相同类型的验证规则.这在本地工作正常,但在部署到服务器时就不行了.怎么回事?
I'm not using the same type of validation rule twice here. This works fine locally, but not when deployed to the server. What's the deal?
我确实添加了对 DataAnnotationExtensions
(http://dataannotationsextensions.org)的引用,这可能是导致有问题吗?
I did add a reference to DataAnnotationExtensions
(http://dataannotationsextensions.org), could that be causing an issue?
删除引用并没有解决问题.IIS配置好像有问题?
removing the reference did not fix the problem. It seems something may be messed up with the IIS configuration?
推荐答案
JimmiTh 对这个问题的评论为我提供了一个关键的见解,让我自己解决这个问题.
JimmiTh's comment on the question provided a key insight for me to resolve this for myself.
就我而言,我确实向 ModelValidatorProviders
添加了一个额外的提供程序.我在我的 Global.asax.csFluent Validation)/em> 文件:
In my case, I definitely did add an additional provider to ModelValidatorProviders
. I added a custom validation factory (using Fluent Validation) with this code in my Global.asax.cs file:
ModelValidatorProviders.Providers.Add(
new FluentValidationModelValidatorProvider(validatorFactory));
但是使用多个提供程序并不必然有问题.似乎有问题的是,如果多个提供程序提供相同的验证器,因为这将多次注册相同的规则,从而导致上述 Microsoft 不显眼的验证代码出现问题.
But using multiple providers isn't necessarily problematic. What seems to be problematic is if multiple providers provide the same validators, because that will register the same rules multiple times, causing the mentioned problem with the Microsoft unobtrusive validation code.
我最终从同一个文件中删除了以下行,因为我决定不需要同时使用这两个提供程序:
I ended up removing the following line from the same file as I decided I didn't need to use both providers:
FluentValidationModelValidatorProvider.Configure();
上面的 Configure
方法本身就是向 ModelValidatorProviders
添加一个提供者,我有效地注册了两次相同的验证器类,因此出现了关于非唯一验证类型"的错误名称".
The Configure
method above is itself adding a provider to ModelValidatorProviders
, and I was effectively registering the same validator class twice, hence the error about non-unique "validation type names".
SO 问题 jquery - Fluent 验证.错误:不显眼的客户端验证规则中的验证类型名称必须是唯一的 指向使用多个提供程序可能导致上述问题的另一种方式.每个提供者都可以配置为向值类型"添加隐式必需属性"(即,不可为空的视图模型属性).为了解决这个特定问题,我可以将代码更改为以下内容,以便所有提供程序都不会添加隐式必需属性:
The SO question jquery - Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique points to another way that using multiple providers can lead to the mentioned problem. Each provider can be configured to add an 'implicit required attribute to 'value types' (i.e. view model properties that aren't nullable). To resolve this particular issue, I could change my code to the following so that none of the providers add implicit required attributes:
FluentValidationModelValidatorProvider.Configure(
provider => provider.AddImplicitRequiredValidator = false);
DependencyResolverValidatorFactory validatorFactory =
new DependencyResolverValidatorFactory();
FluentValidationModelValidatorProvider validatorFactoryProvider =
new FluentValidationModelValidatorProvider(validatorFactory);
validatorFactoryProvider.AddImplicitRequiredValidator = false;
ModelValidatorProviders.Providers.Add(validatorFactoryProvider);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
这篇关于不显眼的客户端验证规则中的验证类型名称必须是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!