我試圖測試出爲實現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