我有一个api方法,我将“/ api / stocks / 1 / images /”和stockimage列表作为json传递。我得到下面的stockImages为null(函数的输入参数)
[Route("api/stocks/{stockId}/images")]
public IHttpActionResult Post(int stockId, [FromBody]List<StockImage> stockImages)
{
return Ok();
}
当我将/api/stocks/1/images/
和stockimage列表作为json
传递并将其包装在一个类中而不是直接接受列表时,我能够看到stockImages预填充(函数的输入参数)
我有一个api方法
[Route("api/stocks/{stockId}/images")]
public IHttpActionResult Post(int stockId, [FromBody]Temp stockImages)
{
return Ok();
}
public class Temp
{
public List<StockImage> stockImages {get; set;}
}
我是否需要拥有此Temp包装类或有什么方法可以避免这种情况?
添加样本json:
{
"stockImages":[
{
"imgId" : 8908,
"imgURL": "http://imgd5.aeplcdn.com/cw/Volkswagen-Polo-Comfortline-4319619.jpg",
"altText":"Honda City Exterior Photos",
"title":"Honda City Exterior Photos",
"defaultImg":true
}
]
}
答案 0 :(得分:1)
所以问题是你将数组包装在一个对象中。删除它,你很高兴
[
{
"imgId" : 8908,
"imgURL": "http://imgd5.aeplcdn.com/cw/Volkswagen-Polo-Comfortline-4319619.jpg",
"altText":"Honda City Exterior Photos",
"title":"Honda City Exterior Photos",
"defaultImg":true
}
]