i have problem model validation in asp.net web api. model have problem:
public sealed class accesstokenrequest { [required] public guid gameid { get; set; } [required] public string gameaccesstoken { get; set; } [range(0, int.maxvalue)] public int? lifetime { get; set; } } when passed string cannot convert guid gameid, return 2 validation errors. 1 is:
the value 'xxxxxxxxxxxxxxx' not valid gameid.
and is:
the value required.
i want first 1 return. latter not make sense since value provided.
thanks in advance.
to honest not sure whether is possible without making custom validation attribute.
the problem providing invalid guid, since cannot parsed correctly, gives null result. binder tries assign null property [required] attribute, gives the value required. error.
let's take [required] attribute:
public override bool isvalid(object value) { if (value == null) return false; string str = value string; if (str != null && !this.allowemptystrings) return str.trim().length != 0; return true; } as can see there check null value.
you can either write new attribute scratch or derive requiredattribute , override isvalid method.
Comments
Post a Comment