Jes*_*ica 8 wcf attributes datamember
我希望在我的WCF数据协定成员上放置属性以验证字符串长度,并可能使用正则表达式进行更细粒度的参数验证.
我可以为数字和DateTime值设置[Range]属性,并且想知道是否有人找到了我可以用于数据验证的任何其他WCF数据成员属性.我发现Silverlight有很多属性,但WCF却没有.
Pra*_*thi 19
添加System.ComponentModel.DataAnnotations对项目的引用.
该引用提供了一些DataAnnotations,它们是:
RequiredAttribute, RangeAttribute, StringLengthAttribute, RegularExpressionAttribute
你可以在你的datacontract如下.
[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(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", ErrorMessage = "Invalid Mail id")]
public string Email { get; set; }
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
24549 次 |
| 最近记录: |