1

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

4

1 回答 1

0

下面是我项目中的代码,看看它是否对你有帮助,因为它的工作和实施

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;
}
于 2013-02-01T07:06:18.450 回答