Web API POST未绑定

Web API POST未绑定,post,binding,asp.net-web-api,fiddler,Post,Binding,Asp.net Web Api,Fiddler,我有一个特殊的请求类,里面只有一把钥匙 public class SpecialRequest { public string Key { get; set; } } 简单控制器 public class ValuesController : ApiController { // GET api/values public SpecialRequest Get() { return new CodeRequest {SecretKey = "AB

我有一个特殊的请求类,里面只有一把钥匙

public class SpecialRequest
{
    public string Key { get; set; }
}
简单控制器

public class ValuesController : ApiController
{
    // GET api/values
    public SpecialRequest Get()
    {
        return new CodeRequest {SecretKey = "ABSDCV"};
    }

    // POST api/values
    public string Post(SpecialRequest specialRequest)
    {
        string temp = specialRequest.Key + "-TESTING";
        return temp;
    }
}
使用fiddler,我将POST发送到POST,我可以看到它在调试器中被命中,但是POST的主体没有绑定到POST方法中的specialRequest参数

Fiddler Headers :
User-Agent: Fiddler
Host: localhost:64180
Content-Length: 66
Content-Type: text/xml


Request Body:
<CodeRequest>
   <SecretKey>
      Hello 
   </SecretKey>
</CodeRequest>
Fiddler标题:
用户代理:Fiddler
主机:本地主机:64180
内容长度:66
内容类型:text/xml
请求机构:
你好

我忘记做什么了?

1-您需要将[FromBody]添加到您的方法参数中

// GET api/values/5
public SpecialRequest Get(int id)
{
    return new SpecialRequest {Key = "ABC-Key"};
}

// POST api/values
[HttpPost]
public void Post([FromBody]SpecialRequest specialRequest)
{
    specialRequest.Key += "Testing";
    // Do something
}
2-您的请求/内容类型应为正确的xml:

一,。内容类型应为“内容类型:应用程序/xml”而不是文本/xml

二,。该机构应:

<SpecialRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebApplication2.Controllers">
    <Key>ABC-Key</Key></SpecialRequest>

ABC键
这肯定会解决它

希望有帮助

编辑
只需确保更改传递的xml中的名称空间以匹配您的项目名称空间,因此本例中我的名称空间是WebApplication2.Controllers,请确保将其更改为您的控制器名称空间。

然后您在其他地方执行其他操作,这会停止工作,因为我有一个发布此代码的工作演示,除了定义SpecialRequest类,然后定义上面提到的方法,最后运行应用程序,使用fiddler,我使用Post,使用与上面完全相同的主体,你只需要更改xmlns字符串中的名称空间来匹配你的应用程序名称空间?让我知道你怎么了。