简体   繁体   English

在 C# Web API 中,当我在 Postman 中发布多个数据时,它返回“null”值

[英]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.如何发布多个 JSON 数据。
  2. I want to Post data by using JSON format.我想使用 JSON 格式发布数据。

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.

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