Custom data annotations with webservice in ASP.NET 3.5
I can't create a custom ValidationAttribute in asp.net 3.5 webservice. Everything seems ok, it compiles and it runs, it just doesn't enter the custom attribute i created. Msdn documentation (http://msdn.microsoft.com/en-us/library/cc668224(v=vs.90).aspx) states that I have to override the IsValid method to get it validated, still it doesn't enter the IsValid. I guess there's a configuration problem and i can't see any other flaw in the code atm.
So, here's the webservice code
[WebMethod]
[Authorize("me")]
public string test()
{
return "Here I am";
}
and this is the ValidationAttribute
public class AuthorizeAttribute : ValidationAttribute { public string me { get; private set; } public AuthorizeAttribute(string me) { if (string.IsNullOrEmpty(me)) { throw new ArgumentNullException("Not me!"); }
this.me = me;
}
public override bool IsValid(object value)
{
bool result = false;
return result;
}
}
I would say that before the webmethod executes it goes into the Constructor(at least that!) and then into the IsValid method, but it doesn't.
Can you say where's the error?
Thanks
/Sball
Aaaaand seems i cannot format this question properly. Sorry
以下是我项目的代码,看看它是否能够帮助你实现这个目标
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
var property = validationContext.ObjectInstance.GetType()
.GetProperty(_fieldName);
var propertyValue = property.GetValue(validationContext.ObjectInstance, null);
if (Validate(propertyValue))
{
return new ValidationResult(
string.Format(ErrorMessageString, validationContext.DisplayName));
}
}
return ValidationResult.Success;
}
链接地址: http://www.djcxy.com/p/9702.html