[英]In C# Web API, when I POST multiple data in the Postman it returns 'null' value
` `
[HttpGet]
public List<studTB> StudentsList()
{
StudentEntities studentEntities = new StudentEntities();
return studentEntities.studTBs.ToList();
}
// POST api/values
[HttpPost]
public IEnumerable<studTB> Post([FromBody] studTB stud)
{
List<studTB> studentEntities = new List<studTB>();
studentEntities.Add(stud);
return studentEntities;
}
In Postman, your body is an array of studTB
objects, but in C#, your method parameter is a single studTB
object.在 Postman 中,您的 body 是一个studTB
对象数组,但在 C# 中,您的方法参数是单个studTB
对象。 Those need to match.那些需要匹配。
Either only post one at a time (as a JSON object, no array), or change the C# to accept a list.一次只发布一个(作为 JSON 对象,没有数组),或者更改 C# 以接受列表。
public IEnumerable<studTB> Post([FromBody] List<studTB> studs)
{
List<studTB> studentEntities = new List<studTB>();
studentEntities.AddRange(studs); // change to Range to add multiple
return studentEntities;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.