C# 空属性不会导致验证失败,尽管StringLength属性
我有一个名为MyMethodQuery的类,它包含我的web方法的entry属性C# 空属性不会导致验证失败,尽管StringLength属性,c#,web-services,validation,datacontractserializer,C#,Web Services,Validation,Datacontractserializer,我有一个名为MyMethodQuery的类,它包含我的web方法的entry属性 [DataContract()] public class MyMethodQuery { [DataMember(IsRequired = true)] [StringLength(100, MinimumLength = 10)] public string Id{ get; set; } } 我的Web方法: public string MyMethod(MyMethodQuery
[DataContract()]
public class MyMethodQuery
{
[DataMember(IsRequired = true)]
[StringLength(100, MinimumLength = 10)]
public string Id{ get; set; }
}
我的Web方法:
public string MyMethod(MyMethodQuery MyMethodQuery)
{
return "it's ok !";
}
如果我离开空属性,我想抛出一个异常。但不管属性是StringLength
,它都不起作用
我用SoapUI测试我的方法。这些属性不应该在这种“开箱即用”的上下文中工作。类是验证世界的入口点,您必须手动调用它 有关示例,请参见。
[DataContract()]
[DataContract()]
public class MyMethodQuery: IValidatableObject
{
[DataMember(IsRequired = true, EmitDefaultValue = false)]
[StringLength(500, MinimumLength = 5)]
public string Id { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Id.Length < 1)
{
yield return new ValidationResult("error");
}
}
}
公共类MyMethodQuery:IValidatableObject
{
[DataMember(IsRequired=true,EmitDefaultValue=false)]
[StringLength(500,最小长度=5)]
公共字符串Id{get;set;}
公共IEnumerable验证(ValidationContext ValidationContext)
{
如果(Id长度<1)
{
返回新的ValidationResult(“错误”);
}
}
}
但是如果Id
为空,则我没有返回错误