我想将 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参数截图
正如 Gabriel Luci 所说,Json 对象最好在请求正文中传递。
关于c# - 如何将 JSON 对象作为参数从 Postman 传递到 ASP.NET WEB API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53842946/