如何使用 webinvoke 方法(Post 或 PUT)在 WCF Rest 中传递多个主体参数
我在 WCF 中编写了一个 REST 服务,其中创建了一个方法(PUT)来更新用户。对于此方法,我需要传递多个主体参数,
[WebInvoke(Method = "PUT", UriTemplate = "users/user",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool UpdateUserAccount(User user,int friendUserID)
{
//do something
return restult;
}
尽管如果只有一个参数,我可以传递用户类的 XML 实体。如下:
var myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
myRequest.Method = "PUT";
myRequest.ContentType = "application/xml";
byte[] data = Encoding.UTF8.GetBytes(postData);
myRequest.ContentLength = data.Length;
//add the data to be posted in the request stream
var requestStream = myRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
但是如何传递另一个参数(friendUserID)值? 谁能帮助我吗?
I have written a REST Service in WCF in which I have created a method(PUT) to update a user. for this method I need to pass multiple body parameters
[WebInvoke(Method = "PUT", UriTemplate = "users/user",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool UpdateUserAccount(User user,int friendUserID)
{
//do something
return restult;
}
Although I can pass an XML entity of user class if there is only one parameter. as following:
var myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
myRequest.Method = "PUT";
myRequest.ContentType = "application/xml";
byte[] data = Encoding.UTF8.GetBytes(postData);
myRequest.ContentLength = data.Length;
//add the data to be posted in the request stream
var requestStream = myRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
but how to pass another parameter(friendUserID) value?
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于除 GET 之外的所有方法类型,只能将一个参数作为数据项发送。因此,要么将参数移动到查询字符串
,要么将参数添加为请求数据中的节点
For all method types except GET only one parameter can be sent as the data item. So either move the parameter to querystring
or add the parameter as node in the request data