gpt4 book ai didi

asp.net-mvc - 派生自 RegularExpressionAttribute 的自定义 DataAnnotations 验证器

转载 作者:行者123 更新时间:2023-12-03 01:58:45 25 4
gpt4 key购买 nike

The Gu provides an example介绍如何创建覆盖 RegularExpressionAttribute 的自定义验证器。

这样做的好处是你不必create a custom Model Validator但我无法让它工作。

给出以下代码:

public class NameAttribute : RegularExpressionAttribute {
public NameAttribute()
: base(@"^[\w\s\-\']+$") {
}
}

这有效:

[RegularExpression(@"^[\w\s\-\']+$")]

但这不是:

[Name]

我是否误解了 Scott 示例的某个方面,或者该示例是否有缺陷,因为 MVC 不支持开箱即用的派生类型,所以实际上我必须创建一个相应的 ModelValidator?

最佳答案

破解了!将以下内容添加到 Global.asax.cs Application_Start()

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(NameAttribute), typeof(RegularExpressionAttributeAdapter));

关于asp.net-mvc - 派生自 RegularExpressionAttribute 的自定义 DataAnnotations 验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2689444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com