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 端点


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01