基于Web的API方法后空参数 [英] Null parameter on web api post method

查看:116
本文介绍了基于Web的API方法后空参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的Web API控制器:

I have a very simple web api controller:

public class CarrinhoController : ApiController
    {
        [HttpPost]
        public string Adiciona([FromBody] string conteudo)
        {
            return "<status>sucesso</status";
        }
    }

现在我正在运行的服务器,并试图测试通过这个方法卷曲是这样的:

Now I'm running the server and trying to test this method via curl like this:

curl --data "teste" http://localhost:52603/api/carrinho

该请求到达我的控制器。然而,参数 conteudo 永远是空的。

我在做什么错了?

感谢。

推荐答案

这些职位详细解释的 http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

在asp.net网站http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

当一个参数有[FromBody],网络API使用Content-Type头,选择格式化。在本实施例中,内容类型是应用/ JSON和请求主体是一个原始JSON字符串(未JSON对象)。

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

目前最一个参数是允许从邮件正文阅读。

At most one parameter is allowed to read from the message body.

添加内容类型:应用程序/ JSON。在提琴手将工作

Add "Content-Type: application/json" on Fiddler will work.

这篇关于基于Web的API方法后空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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