帖子参数总是空 [英] Post parameter is always null
问题描述
由于升级到RC的的WebAPI我有我的WebAPI调用POST当一些真正奇怪的问题。 我甚至又回到新的项目产生的基本版本。所以:
Since upgrading to RC for WebAPI I'm having some real odd issue when calling POST on my WebAPI. I've even gone back to the basic version generated on new project. So:
public void Post(string value)
{
}
和从提琴手呼吁:
Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29
Body:
{
"value": "test"
}
当我调试,字符串值是永远不会被分配到。这只是总是空。 有这个问题的人?
When I debug, the string "value" is never being assigned to. It's just always NULL. Anyone having this issue?
(我第一次看到这个问题有一个更复杂的类型)
(I first saw the issue with a more complex type)
这个问题不仅是必然的ASP.NET MVC 4,出现了RC安装后,一个新的ASP.NET MVC 3项目相同的问题
The problem is not only bound to ASP.NET MVC 4, the same problem occurs for a fresh ASP.NET MVC 3 project after RC installation
推荐答案
既然你只有一个参数,你可以尝试用[FromBody]属性装饰,或者更换接受DTO与值的方法作为属性,我建议在这里: MVC4 RC的WebAPI参数绑定
Since you have only one parameter, you could try decorating it with the [FromBody] attribute, or change the method to accept a DTO with value as a property, as I suggested here: MVC4 RC WebApi parameter binding
更新:官方ASP.NET站点今天更新了一个很好的解释:<一href="http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1">http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1
UPDATE: The official ASP.NET site was updated today with an excellent explanation: http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1
在简单地说,当发送一个唯一的简单的类型在身上,发出正义的价值prefixed等号(=),例如:正文:
In a nutshell, when sending a single simple type in the body, send the just the value prefixed with an equal sign (=), e.g. body:
=测试
这篇关于帖子参数总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!