如何通过邮递员使用JSON对象发布到MVC端点 [英] How to post to MVC endpoint via postman with JSON object

查看:30
本文介绍了如何通过邮递员使用JSON对象发布到MVC端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象,并尝试向我的API端点发出发布请求.

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端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆