从API控制器调用POST方法 [英] Calling POST method from API controller

查看:359
本文介绍了从API控制器调用POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyFIlesController的API控制器.

I have an API controller named MyFIlesController.

在其中,我有此方法:

// POST api/myfiles
public void Post([FromBody]string value)
{
}

这是我用Fiddler称呼的方式:

And here's how I call it with Fiddler:

POST
URL: `http://localhost:58075/api/myfiles`

-------------------------

Request Header:

User-Agent: Fiddler

Host: localhost:58075

Content-Type: application/json

Content-length: 18

-------------

Request body:
{"value": "asjkfsf"}

该方法被调用,但是值是null.我在做什么错了?

The method gets called, but value is null. What am I doing wrong?

推荐答案

创建与您的JSON相对应的类:

Create a class that corresponds to your JSON:

public class Test
{
    public string value{get; set;}
    public int ID {get; set;}
}

然后更改您的Api动作:

And then change your Api-action:

// POST api/myfiles
public void Post([FromBody]Test value)
{

}

如果您不想这样做,只需更改POST-body:

If you don't want to do that, just change the POST-body:

"somevalue"

编辑:将ID添加到POST有效负载中. 现在,您的JSON应该如下所示:

EDIT: Added ID to the POST-payload. Now your JSON should look like this:

{"value": "someval",
"ID": 1}

这篇关于从API控制器调用POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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