2017-09-05 74 views
1

目的是通过是否有可能结合POST请求是如下:通过使用[来自体]属性或它仅与没有“继承”简单的请求工作递归结合从身体属性

{ 
    "name": "Tree Node 1", 
    "children": 
    { 
     "name": "Tree Node 2", 
     "children": 
     { 
      "name": "Tree Node 3", 
      "children": [], 
      "data": 
      { 

      } 
     } 
    } 
} 

回答

1

那么,首先是children一个数组或对象?你使用两者。

我会说这大概应该是一个数组,所以像:

{ 
    "name": "Tree Node 1", 
    "children": 
    [{ 
     "name": "Tree Node 2", 
     "children": 
     [{ 
      "name": "Tree Node 3", 
      "children": [], 
      "data": 
      { 
      } 
     }] 
    }] 
} 

然后你只需做一个模型,引用本身:

public class TreeNode 
{ 
    public string Name { get; set; } 
    public ICollection<TreeNode> Children { get; set; } 
    public IDictionary<string, string> Data { get; set; } 
} 

你没有放任何东西data对象,所以我无法猜测它的结构是什么。在这里我使用了Dictionary<string, string>来使它包含任何键值对。

现在只是让喜欢你的控制器动作:

[HttpPost("test")] 
public IActionResult Test([FromBody] TreeNode model) 
{ 
    return Ok(); 
} 
+0

这两种解决方案的工作,非常感谢。 – bielu000

1

你会需要这样的事:

public class MyClass 
{ 
    public string Name{get;set;} 
    public IEnumerable<MyClass> Children{get;set;} 
    public Data Data {get;set;} 
} 
public class Data 
{ 
    //properties of data 
}