I have a dotnet core
Web API which accepts a body message containing JSON. I have tried writing this in various forms, eg:
[HttpPost]
public async Task<IActionResult> Create([FromBody] dynamic content)
Is difficult to retrieve the properties and does not return the properties by name.
[HttpPost]
public async Task<IActionResult> Create([FromBody] ExpandoObject content)
Is also difficult to retrieve properties from, though top level properties can be retrieved with ["details"]
(though no deeper).
[HttpPost]
public async Task<IActionResult> Create([FromBody] JObject content)
This allows properties to be retrieved quite easily:
var firstName = content["contact"][0]["firstName"];
var accountName = content["generalDetails"]["accountName"];
The body message being sent is something like this and will not change in format.
{
"details": {
"phone": 0123456789,
"email": "test@gmail.com",
"county": "BF"
},
"admin": {
"number": "gb12345"
},
"additional": {},
"contact": [
{
"firstName": "Jeff",
"lastName": "Bezos",
"title": "Mr."
},
{
"firstName": "Steve",
"lastName": "Jobs",
"title": "Mr."
}
]
}
I want to make the body parameter dynamic (in some form) because the actual body content being sent is very large and contains many more properties than those in the example above. It seems prudent to avoid creating a large mapping/DTO class in case the design of the message does change in the future/further down the development path.
Is there a way to retrieve the properties of the dynamic
or ExpandoObject
values?