自从升级到RC的WebAPI,我有一些真正奇怪的问题时调用POST在我的WebAPI。 我甚至回到了在新项目上生成的基本版本。所以:

public void Post(string value)
{
}

提琴手喊道:

Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29

Body:
{
    "value": "test"
}

当我调试时,字符串“value”永远不会被赋值。它总是NULL。 有人有这个问题吗?

(我第一次看到这个问题是在一个更复杂的类型上)

这个问题不仅仅局限于ASP。在asp.net MVC 4中,同样的问题出现在一个新的ASP。NET MVC 3项目后RC安装


当前回答

这个链接帮助了我:http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

基本上,它说你应该为参数使用一个空名称:

public string Post([FromBody]string myParameter){ 
...
}  

$.post("/api/dosomething", { '' : "myvalue" });

其他回答

如果你确定你发送的JSON,那么你必须仔细跟踪你的API:

安装Microsoft.AspNet.WebApi.Tracing包 添加config.EnableSystemDiagnosticsTracing ();在WebApiConfig类的Register方法中。

现在查看Debug输出,您可能会发现一个无效的ModelState日志条目。

如果ModelState无效,你可以在它的Errors中找到真正的原因:

没有人能猜到这样一个例外:

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

这招对我很管用:

Create a C# DTO class, with a property for every attribute you want to pass from jQuery/Ajax public class EntityData { public string Attr1 { get; set; } public string Attr2 { get; set; } } Define the web api method: [HttpPost()] public JObject AddNewEntity([FromBody] EntityData entityData) { Call the web api as such: var entityData = { "attr1": "value1", "attr2": "value2" }; $.ajax({ type: "POST", url: "/api/YOURCONTROLLER/addnewentity", async: true, cache: false, data: JSON.stringify(entityData), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { ... } });

我在《小提琴手》里也遇到了同样的问题。我已经有Content-Type: application/json;请求头中的charset=utf-8或Content-Type: application/json。

我的请求体也是一个普通的字符串,在Fiddler中我写了:{'controller':'ctrl'}。这使得POST方法中的字符串参数为空。

修正:记住使用引号,从而表示字符串。也就是说,我通过编写“{'controller':'ctrl'}”来修复它。(注意:在编写JSON时,要么一定要使用撇号,要么像这样转义引号:"{\"controller\":\"ctrl\"}")。

我知道这不是问题的答案,但我在寻找解决问题的方法时偶然发现了它。

在我的情况下,复杂类型没有被绑定,但我没有做一个POST,我正在做一个GET与查询字符串参数。解决方案是在参数中添加[FromUri]:

public class MyController : ApiController
{
    public IEnumerable<MyModel> Get([FromUri] MyComplexType input)
    {
        // input is not null as long as [FromUri] is present in the method arg
    }
}

尝试创建一个类作为数据模型,然后发送一个具有与数据模型类属性匹配的属性的JSON对象。(注:我已经测试了这个,它与我今天刚刚下载的最新的MVC 4 RC 2012一起工作)。

public HttpResponseMessage Post(ValueModel model)
{
    return Request.CreateResponse<string>(HttpStatusCode.OK, "Value Recieved: " + model.Value);
}

public class ValueModel
{
    public string Value { get; set; }
}

下面的JSON对象以HTTP-POST正文形式发送,内容类型为application/ JSON

{ "value": "In MVC4 Beta you could map to simple types like string, but testing with RC 2012 I have only been able to map to DataModels and only JSON (application/json) and url-encoded (application/x-www-form-urlencoded body formats have worked. XML is not working for some reason" }

我认为必须创建数据模型类的原因是假定简单值来自url参数,而假定单个复杂值来自主体。它们确实有[FromBody]和[FromUrl]属性,但使用[FromBody]字符串值仍然不适合我。看起来他们还在解决很多bug,所以我相信这在未来会改变。

编辑: 让XML在主体中工作。默认的XML序列化器被更改为DataContractSerializer而不是XmlSerializer。在我的全局中放入以下一行。Asax文件修复了这个问题(参考)

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;