在 POST 操作期间将字符串参数传递给 RESTful 服务
我有一个使用以下方法的 RESTful 服务:
[WebInvoke]
string GetDataFromStringAsString(string xmlString);
我的客户端对该方法的调用如下:
var client = new RestClient();
client.BaseUrl = serviceBaseUrl;
var request = new RestRequest(method){RequestFormat = DataFormat.Xml};
request.Resource = resourceUrl;
request.AddParameter("text/xml", requestBody,
ParameterType.RequestBody);
var response = client.Execute(request);
让我们将一个字符串作为“Hello World”发布。
现在我发布到上述方法的字符串给了我 400 Bad 要求。为了让它工作,我必须将上面的字符串包裹起来 如下所示的元素:
<string xmlns="http://schemas.microsoft.com/2003/10/
Serialization/">Hello World</string>
现在,当我发布上面的字符串时,我会收到来自的成功响应 服务器。
为什么我必须手动包裹字符串才能使其工作。是 有一种方法可以让我无需执行以下操作即可发布字符串 以上手动。
I am having a RESTful service with the following method:
[WebInvoke]
string GetDataFromStringAsString(string xmlString);
My client call to the method is as below:
var client = new RestClient();
client.BaseUrl = serviceBaseUrl;
var request = new RestRequest(method){RequestFormat = DataFormat.Xml};
request.Resource = resourceUrl;
request.AddParameter("text/xml", requestBody,
ParameterType.RequestBody);
var response = client.Execute(request);
Let us take a string to post as "Hello World".
Now the string that i post to the above method gives me a 400 Bad
request. In order to get it working i had to wrap the above string in
a element as shown below:
<string xmlns="http://schemas.microsoft.com/2003/10/
Serialization/">Hello World</string>
Now when i post the above string i get a success response back from
the server.
Why is that i have to manually wrap the string to make it work. Is
there a way that i can achieve to post a string without doing the
above manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道的唯一其他方法是使用流作为输入参数。例如,
.Net 4 WCF REST 的问题在于,从根本上来说,WCF 只知道如何传递两种类型的信息,即 XML 或字节流。就我个人而言,我会使用 WCF Web API 而不是标准 WCF REST 库,因为您将遇到更多此类问题。
The only other way that I am aware of is to use stream as your input parameter. e.g.
The problem with .Net 4 WCF REST is that fundamentally WCF only knows how to pass two types of info, either XML or a stream of bytes. Personally, I would use WCF Web API instead of the standard WCF REST library because you are going run into lots more of these kinds of issues.