I have a class in this format:
[XmlRoot("Root")]
public class XYZ
{
public string A{ get; set; }
public string B{ get; set; }
public string C{ get; set; }
public string D{ get; set; }
public ParentClass E{ get; set; }
public string F{ get; set; }
}
There are many child classes of this parent class for property E
, and I need it so that if I pass the properties as JSON or XML in a post request that correspond to the child objects then it should store that as E
But it's only showing properties that are in the ParentClass
I have seen similar posts such as this on the topic: Deserialising Json to derived types in Asp.Net Web API
The problem with the other solutions I have seen is that they seem to expect you to pass the subclass's object type in the request which I don't want to do, it should work it out. And I don't think I should have to specify a list of child objects that it should serialize to either.
Example request body:
{
"E":{
"ParentClassesProperty": "Foobar"
"ChildClassesProperty": "Hello World"
},
"A":"foobar",
"B":"hfj",
"C": "",
"D":"foo",
"F":"bar"
}
Controller method signature:
[HttpPost]
[Route("route")]
[Authorize(Foo = "foo", Bar = "Bar")]
public HttpStatusCode NotifyMetrix(XYZ xyz)