如何使用 webinvoke 方法(Post 或 PUT)在 wcf rest 中传递多个 body 参数 [英] How pass multiple body parameters in wcf rest using webinvoke method(Post or PUT)

查看:61
本文介绍了如何使用 webinvoke 方法(Post 或 PUT)在 wcf rest 中传递多个 body 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

虽然我可以传递一个用户类的XML实体,如果只有一个参数.如下:

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();

但是如何传递另一个参数(friendUserID)值?有人可以帮我吗?

but how to pass another parameter(friendUserID) value? Can anyone help me?

推荐答案

对于除 GET 之外的所有方法类型,只能发送一个参数作为数据项.所以要么将参数移动到查询字符串

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>

这篇关于如何使用 webinvoke 方法(Post 或 PUT)在 wcf rest 中传递多个 body 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆