It would be best to create a class that models the object you expect.
Thus, in your Web API method, you can use the [FromBody] attribute to automatically analyze the request body.
Example -
Your data contract will look like this:
public class MyContract { public string MyData { get; set;} }
In your ApiController
[HttpPost] [Route("api/myobject")] public async Task ReceiveMyObject([FromBody]MyContract object) { var data = object.MyData;
This may seem tedious, but it will allow you to save your code.
Edit So, to create a contract from this:
{ "head": { "action": "create", "object": "oneobject", "user": "theuser" }, "object": { "name1": "a name 1", "name2": "a name 2", "description": "a description here" }, "rule": [{ "name": "any name", "value": "any value" }, { "name": "another name", "value": "another value" }] }
You would do something like this:
public class MyContract { [JsonProperty("head")] public MetaObject Head { get; set; } // Not sure if this will work, but it probably will [JsonProperty("object")] public JObject ExtendedInformation { get; set; } [JsonProperty("rule")] public Rule[] Rules { get; set; } } // "MetaObject" definition omitted but you can understand my point with the below public class Rule { [JsonProperty("name")] public string Name { get; set; } [JsonProperty("value")] public string Value { get; set; } }
Julius May 25 '16 at 21:11 2016-05-25 21:11
source share