How to post to MVC endpoint via postman with JSON object(如何使用 JSON 对象通过邮递员发布到 MVC 端点)
问题描述
我有一个 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 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 JSON 对象通过邮递员发布到 MVC 端点
基础教程推荐
- 从 C# 控制相机设备 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
