来自body value的web api为空?

时间:2014-11-06 21:25:49

标签: c# asp.net asp.net-web-api asp.net-web-api2

我在这里检查了类似的问题:

web-api POST body object always null

这与我的方案中发生的情况并不完全一致。我正在使用以下请求标头内容类型向我的api发出请求:

Key Value
Content-Type    application/x-www-form-urlencoded

我的请求正文是我从其他服务获得的响应,我无法更改

Response=SomeLongStringOfBAse64EncodedData

我可以通过设置断点来查看我的Api Post方法,因为:

public HttpResponseMessage Authenticate([FromBody]string Response) 

然而,响应字符串总是变为空值,即使我在请求正文中看到它我也无法理解。

1 个答案:

答案 0 :(得分:0)

对于像这样的行动方法

public HttpResponseMessage Authenticate([FromBody]string Response)

你需要拥有这样的请求体,才能使绑定工作。

=SomeLongStringOfBAse64EncodedData

如果要绑定Response=SomeLongStringOfBAse64EncodedData,则需要更改此方法。

public HttpResponseMessage Authenticate(SomeClass response)

并添加一个类

public class SomeClass
{
    public string Response { get; set; }
}