2013-01-16 43 views
0

我創建了一個非常適合get的restful wcf服務。現在我建立了一些post方法。客戶端以JSON格式將數據發送到此服務。服務方法不會有任何參數,所以我需要從請求中讀取jsondata。我無法弄清楚我應該如何檢索從請求中獲得的數據。WCF Rest服務如何使用輸出參數訪問post JSON數據

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = "/SaveEmployee", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 

public bool SaveEmployee() 
     { 
      //Capture Employee Object Data here and perform save 
      return true; 
     } 

     [DataContract] 
     public class Employee 
     { 
      [DataMember] 
      public int Id 
      { 
       get; 
       set; 
      } 

      [DataMember] 
      public string Name 
      { 
       get; 
       set; 
      } 
     } 

回答

0

如果您使用數據合約類型設置您的方法,它將填充請求主體併爲您解序列化。

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = "/SaveEmployee", RequestFormat =   WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public bool SaveEmployee(Employee sentEmployee) 
    { 
     //Capture Employee Object Data here and perform save 
     return true; 
    } 

如果你不能這樣做,你將不得不通過URL參數傳遞json數據並手動反序列化內容。