问题描述
我有一个 JSON 对象并试图向我的 API 端点发送一个 post 请求.
I have a JSON object and trying to have a post request to my API endpoint.
在我的列表控制器中,我有以下功能
In my Listing controller I have the following function
[HttpPost]
public async Task<IActionResult> Import(GetImportInput input)
{
return StatusCode(200);
}
GetImportInput.cs
GetImportInput.cs
public string Name {get; set;}
邮递员详情:
Postman details:
ContentType = application/json
Body = {
"name" : "Rabbit"
}
当我在 Import 方法中放置断点时,断点命中,但参数输入没有值 Rabbit
.请问如何正确让邮递员发送正文,以便我的控制器方法将其接收.
When I put a breakpoint inside my Import method, the breakpoint hits, but the parameter input does not have the value Rabbit
. May I ask how do I properly get my postman to send the body so my controller method will pick it up.
标题标签
推荐答案
您的控制器方法中缺少 [FromBody],这里大小写不是问题.请记住在使用 Postman 进行测试时使用标头 Content-Type: application/json.
You are missing [FromBody] in your controller method, casing is not an issue here. Remember to use header Content-Type: application/json when testing this with Postman.
public class ListingController : ControllerBase
{
[HttpPost]
public IActionResult Import([FromBody]GetImportInput input)
{
return StatusCode(200);
}
}
这篇关于如何使用 JSON 对象通过邮递员发布到 MVC 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!