数据成员属性数据验证 [英] DataMember attributes for Data validation
问题描述
我要寻找的地方属性在我的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.
我可以在[范围]属性的数值和日期值,并想知道如果你们发现任何其他WCF数据成员属性我可以用数据验证。我已经找到了Silverlight的,但不是为WCF属性的bevvy。
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
引用您的项目。
该参考提供了一些DataAnnotations它们是:
The reference provides some DataAnnotations which are:
RequiredAttribute标签,RangeAttribute,StringLengthAttribute,RegularEx pressionAttribute
你可以在你datacontract像下面。
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(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", ErrorMessage = "Invalid Mail id")]
public string Email { get; set; }
希望这有助于。
这篇关于数据成员属性数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!