c# - .Net Core Web API: Posting complex type -
i experiencing issue while developing restful webapi .net core, can't consume http post method inserts complex type object without specifying parameter "frombody". accordingly documentation microsoft...
parameter binding in asp.net web api
... when using complex types on http post don't have specify [frombody]
data annotation, should work, when try consume method webapi...
// post api/checker/ [httppost] public int insert(entity e) { console.writeline($"entity: {e.id} - {e.name}"); }
... entity has id , name, client method ...
string json = jsonconvert.serializeobject(entity); httpclient client = new httpclient(); httpresponsemessage message = await client.postasync( "http://localhost:5000/api/checker", new stringcontent(json, encoding.utf8, "application/json"));
... object reaches webapi values 0 (id)
, null (name)
, unless when webapi method signature uses [frombody]
annotation. missing here? i've done research , couldn't figure out.
[edited]:
this entity class:
public class entity { public int id { get; set; } public bool isdeleted { get; set; } public string name { get; set; } }
... serialized json using newtonsoft's json.net ...
{"id":99,"isdeleted":false,"name":"testchecker"}
... output insert webapi method ...
entity: 0 -
there split/change in asp.net core previous versions mvc & web api controllers merged together.
the default behavior in asp.net core, post requests, it data in request body using form data (x-www-form-urlencoded). have explicit if want find appliction/json body json.
this link might details: https://andrewlock.net/model-binding-json-posts-in-asp-net-core/
Comments
Post a Comment