简体   繁体   中英

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;
        }

` 在此处输入图像描述 在此处输入图像描述

  1. How to POST multiple JSON data.
  2. I want to Post data by using JSON format.

In Postman, your body is an array of studTB objects, but in C#, your method parameter is a single studTB object. 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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM