基于Web的API方法后空参数 [英] Null parameter on web api post method
问题描述
我有一个非常简单的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屋!