c# - 如何将 JSON 对象作为参数从 Postman 传递到 ASP.NET WEB API

标签 c# json asp.net-core-mvc postman asp.net-core-webapi

我想将 json 对象作为查询字符串参数(不是来自正文)从 postman 传递到 ASP.NET Core Web API url。 请告诉我如何通过? 下面是示例 JSOB 对象结构:

这里,'names'是字符串数组

"students":[
      {
         "id":"1",
         "names":["john", "james"]
      },
      {
         "id":"2",
         "names":["peter", "harry"]
     }
]

最佳答案

这是我做的一个demo,你可以引用一下

在 Postman 中,记得将 Headers 中的“Content-Type”设置为“application/json”,否则你可能会得到一个错误 - 415 不支持的媒体类型。

https://localhost:44388/api/student/?students[0].id=1&students[0].name[0]=john&students[0].name[1]=james&students[1].id=2&students[1].name[0]=peter&students[1].name[1]=harry

学生模型

 public class Student
{
    public int Id { get; set; }
    public string[] Name { get; set; }
}

在 Controller 中,不要忘记在 action 的参数中添加 [FromQuery]

[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
    [HttpPost]
    public void PostStudent([FromQuery]List<Student> students)
    {
    }
}

students参数截图

enter image description here

正如 Gabriel Luci 所说,Json 对象最好在请求正文中传递。

关于c# - 如何将 JSON 对象作为参数从 Postman 传递到 ASP.NET WEB API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53842946/

相关文章:

c# - 使用 Microsoft.Net.Http 将文件发送到服务

c# - 将 JSON 序列化为动态时属性名称中的非法字符

c# - ASP.NET Core 中必需的查询字符串参数

javascript - 需要点击超链接才能访问代码

c# - 需要帮助理解 _set_security_error_handler()

javascript - 将 JSON 数据保存到数据库

javascript - 使用 mobileFirst javascript 适配器读取本地服务器 .json 文件

android - Android 4.4.4 上的 FlexJson ClassNotFound 异常

c# - Ajax 调用正在发布 0 或 null

c# - 如何使用 Visual Studio 2019 在 ASP.NET Core 2.2 中安装 Font Awesome