2017-05-18 33 views
0

我想問問某人,如何更正將類的集合準備爲[FromBody]的綁定值。來自POST的ASP.NET API綁定值

我有三類:

public class Point 
{ 
public int? X {get; set;} 
} 

public class CollectionPoint 
{ 
public List<Point> Point {get; set;} 
} 

public class TestBlock 
{ 
public string? Name {get; set;} 
public List<CollectionPoint> CollectionPoint {get; set;} 
} 

比我的方法讀取JSON對象,如:

public string Post([FromBody] TestBlock testBlock) 

最後,我測試JSON,如:

{ 
    "Name":"Block1", 
    "CollectionPoint": 
    [ 
     { 
     "Point":{"X":"20"}, 
     "Point":{"X":"22"}, 
     "Point":{"X":"25"} 
     }, 
     { 
     "Point":{"X":"40"} 
     } 
    ] 
} 

但問題是,這種解決方案不起作用。有人能幫我嗎?謝謝。

回答

0

這個問題不是很清楚。 你想收集點數嗎? 如果是這樣,有額外的類somwhere。

public class Point 
{ 
public int? X {get; set;} 
} 

public class TestBlock 
{ 
public string? Name {get; set;} 
public List<Point> CollectionPoint {get; set;} 
} 

你想收集點的集合嗎? 如果是這樣的JSON格式不正確,應該是:

{ 
    "Name":"Block1", 
    "CollectionPoint": 
    [ 
     { 
      "Point": 
      [ 
       {"X":"20"}, 
       {"X":"22"}, 
       {"X":"25"} 
      ] 
     }, 
     { 
      "Point": 
      [ 
       {"X":"20"} 
      ] 
     } 

    ] 
} 
+0

是的,我需要CollectionPoint的收集和這個對象可以containt點集合。我嘗試你的JSON例子,但沒有結果。 – user3120717

+0

我用正確的JSON更新了我的答案。 – anserk