2011-03-12 18 views
7

我已經在WCF中編寫了一個REST服務,其中我創建了一個用於更新用戶的方法(PUT)。該方法我需要通過多個身體參數如何使用webinvoke方法(Post或PUT)在wcf rest中傳遞多個body參數

[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)值? 任何人都可以幫助我嗎?

回答

11

對於除GET以外的所有方法類型,只能將一個參數作爲數據項發送。因此,無論移動參數來查詢字符串

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

或在請求數據

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

由於阿米特添加參數作爲節點。我做了同樣的,它的工作正常:) – 2011-03-12 09:32:21

相關問題