加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 自定义DataAnnotations Validator派生自RegularE

发布时间:2020-12-16 03:57:05 所属栏目:asp.Net 来源:网络整理
导读:The Gu provides an example如何创建覆盖RegularExpressionAttribute的自定义验证器. 这样做的好处是你不必使用create a custom Model Validator,但我无法让它工作. 给出以下代码: public class NameAttribute : RegularExpressionAttribute { public NameA
The Gu provides an example如何创建覆盖RegularExpressionAttribute的自定义验证器.

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

给出以下代码:

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

这有效:

[RegularExpression(@"^[ws-']+$")]

但这不是:

[Name]

我是否误解了Scott示例的一个方面,或者说MVC不支持派生类型的示例存在缺陷,所以实际上我必须创建一个相应的ModelValidator?

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读