c# - WebAPI2 Json.Net Required property not adding ModelState Error properly -


[datacontract(namespace="")] public class value {     [datamember(isrequired=true)]     public string id { get; set; }      [datamember(isrequired=true)]     public int num { get; set; }      [datamember]     public string name { get; set; } }          public value post(value value) {     if(!modelstate.isvalid)     {         //bad request     }     return value; } 

i trying enforce values specified in post request web api. in value model above, when num property omitted:

 {"id": "abc", "name":"john"} 

it adds error model state indicating absence. however, when id property omitted:

 {"num" : 3, "name" : "john"} 

unexpectedly, no model state error added, , model considered valid.

when manually deserialize model jsonconvert.deserialize throws serialization exception in both cases indicating property missing. why appear add model state errors when value type (int) not present correctly, not when reference type (string) missing request body? how can include in model state errors?

note: not enough put [required] attribute on id property. want allow null or empty string value posted, long included in request.

try this?

[datamember] [newtonsoft.json.jsonproperty(required = newtonsoft.json.required.allownull)] public string id { get; set; } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -