2014-12-02 83 views
1

我試圖測試出爲實現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]字符串值。

+0

看看這個類似的問題:http://stackoverflow.com/questions/21618471/web-api-put-is-識別 - 查詢字符串但不是身體 – Milen 2014-12-02 09:46:05

回答

1

正如你可以在this中看到的,當我使用[FromBody]時,你應該先使用=,然後使用參數的值。

所以你的情況嘗試

byte[] buffer = Encoding.ASCII.GetBytes("=TEST_STRING"); 

,而不是

byte[] buffer = Encoding.ASCII.GetBytes("TEST_STRING"); 
+0

非常感謝:) – DafaDil 2014-12-02 14:13:50

+0

高興地幫助你:) – faby 2014-12-02 14:28:11