How to pass multiple body parameters to wcf rest using the webinvoke method (Post or PUT)

I wrote a REST service in WCF in which I created a (PUT) method to update a user. for this method I need to pass several 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 the XML object of the user class if there is only one parameter. in the following way:

 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 value (friendUserID)? Can anybody help me?

+7
source share
1 answer

For all types of methods except GET, only one parameter can be sent as a data element. So either move the parameter to querystring

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

or add the node parameter to the request data

 <UpdateUserAccount xmlns="http://tempuri.org/"> <User> ... </User> <friendUserID>12345</friendUserID> </UUpdateUserAccount> 
+11
source

All Articles