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

荒凉一梦 提交于 2019-12-06 22:40:17

问题


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?


回答1:


For all method types except GET only one parameter can be sent as the data item. 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 parameter as node in the request data

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


来源:https://stackoverflow.com/questions/5281148/how-pass-multiple-body-parameters-in-wcf-rest-using-webinvoke-methodpost-or-put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!