用于数据验证的 DataMember 属性 [英] DataMember attributes for Data validation

查看:15
本文介绍了用于数据验证的 DataMember 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的 WCF 数据协定成员上放置属性以验证字符串长度,并可能使用正则表达式进行更精细的参数验证.

I am looking to place attributes on my WCF data contract members to validate string length and possibly use regex for more granular parameter validation.

我可以将 [Range] 属性用于数值和 DateTime 值,并且想知道你们中是否有人找到了我可以用于数据验证的任何其他 WCF 数据成员属性.我发现了 Silverlight 的一些属性,但 WCF 没有.

I can the [Range] attribute for numeric and DateTime values and was wondering if any of you have found any other WCF Data Member attributes I can use for data validation. I have found a bevvy of attributes for Silverlight but not for WCF.

推荐答案

System.ComponentModel.DataAnnotations 引用添加到您的项目.

Add System.ComponentModel.DataAnnotations reference to your project.

该参考提供了一些 DataAnnotations,它们是:

The reference provides some DataAnnotations which are:

RequiredAttribute、RangeAttribute、StringLengthAttribute、RegularExpressionAttribute

您可以在您的数据合同中如下所示.

you can in your datacontract like below.

    [DataMember]
    [StringLength(100, MinimumLength= 10, ErrorMessage="String length should be between 10 and 100." )]
    [StringLength(50)]     // Another way... String max length 50
    public string FirstName { get; set; }

    [DataMember]
    [Range(2, 100)]
    public int Age { get; set; }

    [DataMember]
    [Required]
    [RegularExpression(@"[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}", ErrorMessage = "Invalid Mail id")]
    public string Email { get; set; }

希望这会有所帮助.

这篇关于用于数据验证的 DataMember 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆