Post Array Json to .net core web api controller [duplicate]

孤人 提交于 2020-02-21 10:36:29

问题


I have this code

    [HttpPost("[action]")]
    public IActionResult Add([FromBody] Player player)
    {
        PlayerService.Add(player);
        PlayerService.SaveChanges();
        return Created("Player created",player.Name);
    }

and this json

[
  {
    "name": "Olivier Giroud",
    "league": "Premier League",
    "currentTeam": "Arsenal"
  }
]

and everything is going ok. But if i want to send a json array

[ 
  { "name": "Olivier Giroud", "league": "Premier League", "currentTeam": "Arsenal" },    
  {"name": "Lucas Perez","league": "Premier League","currentTeam":"Arsenal"}
]

i get an exception "Object reference not set to an instance of an object.' ". I tried with List

public IActionResult Add([FromBody] List< Player >  players)    

or

public IActionResult Add([FromBody] IEnumerable < Player >  players)

or

public IActionResult Add([FromBody] Players[] players)

but without success. What should i do?


回答1:


Try changing

public IActionResult Add([FromBody] Players[] players)

To

public IActionResult Add([FromBody] Player[] players)


来源:https://stackoverflow.com/questions/45393041/post-array-json-to-net-core-web-api-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!