我试图测试出为实现REST服务如下:FromBody字符串输入REST Web服务没有约束力
public bool loadData(string file, string page, string mapping, [FromBody]string value)
{
// Implementation
}
我使用调用该服务的代码:
uri = "http://localhost:9576/API/DataLoaderService?file=F&page=P&mapping=M";
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Method = Method.ToUpper();
byte[] buffer = Encoding.ASCII.GetBytes("TEST_STRING");
req.ContentLength = buffer.Length;
req.ContentType = "application/x-www-form-urlencoded";
Stream PostData = req.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
然而, “TEST_STRING”不会绑定到[FromBody]字符串值。
看看这个类似的问题:http://stackoverflow.com/questions/21618471/web-api-put-is-识别 - 查询字符串但不是身体 – Milen 2014-12-02 09:46:05