如何使用webinvoke方法(Post或PUT)在wcf rest中传递多个body参数

Raj*_*mar 8 wcf webinvoke parameter-passing

我在WCF中编写了一个REST服务,我在其中创建了一个方法(PUT)来更新用户.对于这种方法,我需要传递多个身体参数

[WebInvoke(Method = "PUT", UriTemplate = "users/user",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool UpdateUserAccount(User user,int friendUserID)
{
    //do something
    return restult;
}
Run Code Online (Sandbox Code Playgroud)

虽然如果只有一个参数,我可以传递用户类的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();
Run Code Online (Sandbox Code Playgroud)

但是如何传递另一个参数(friendUserID)值?谁能帮我?

ami*_*t_g 12

对于除GET之外的所有方法类型,只能将一个参数作为数据项发送.因此要么将参数移动到querystring

[WebInvoke(Method = "PUT", UriTemplate = "users/user/{friendUserID}",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool UpdateUserAccount(User user, int friendUserID)
{
    //do something
    return restult;
}
Run Code Online (Sandbox Code Playgroud)

或者将参数添加为请求数据中的节点

<UpdateUserAccount xmlns="http://tempuri.org/">
    <User>
        ...
    </User>
    <friendUserID>12345</friendUserID>
</UUpdateUserAccount>
Run Code Online (Sandbox Code Playgroud)