2014-12-02 82 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