2012-02-17 50 views
1

我爲我的REST項目使用WCF Rest 40(CS)模板。如何在WCF Rest中傳遞Json對象?

http://localhost:8525/Device/Login?deviceID=testid&password=a&serialNum=testserial 

但通過JSON數據的時候,我得到:

HTTP/1.1 400 Bad Request 
Server: ASP.NET Development Server/10.0.0.0 
Date: Fri, 17 Feb 2012 08:41:38 GMT 
X-AspNet-Version: 4.0.30319 
Content-Length: 1647 
Set-Cookie: ASP.NET_SessionId=4tez22jvuy0s3tiioctukvdq; path=/; HttpOnly 
Cache-Control: private 
Content-Type: text/html 
Connection: Close 

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Request Error</title> 
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style> 
    </head> 
    <body> 
    <div id="content"> 
     <p class="heading1">Request Error</p> 
     <p>The server encountered an error processing the request. See server logs for more details.</p> 
    </div> 
    </body> 
</html> 

方法不被允許使用招
(響應
傳遞數據時,通過URL我沒有得到任何問題)

內線服務:

namespace WCFRest.Rest 
{ 
    [ServiceContract] 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class DeviceComponent 
    { 
     [WebInvoke(UriTemplate = "Login?deviceID={deviceID}&password={password}&serialNum={serialNum}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
     public WRModel.Entities.Session Login(string deviceID, string password, string serialNum) 
     { 
      string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
      WRModel.Interfaces.IDeviceComponent svc = WRModel.Logic.ConfigAndResourceComponent.Containers().Resolve<WRModel.Interfaces.IDeviceComponent>(); 
      return svc.Login(deviceID, password, serialNum, ip); 
     } 

     [WebInvoke(UriTemplate = "Logout", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
     public bool Logout(WRModel.Entities.DeviceSession deviceSession) 
     { 
      WRModel.Interfaces.IDeviceComponent svc = WRModel.Logic.ConfigAndResourceComponent.Containers().Resolve<WRModel.Interfaces.IDeviceComponent>(); 
      return svc.Logout(deviceSession); 
     } 
    } 
} 

The 登錄部分工作正常,但註銷導致錯誤。我哪裏做錯了?感謝幫助。這裏是一些圖像使用招:

enter image description here

編輯

DeviceSession實體

[DataContract(Name = "DeviceSession", Namespace = "http://MySite.com/1.0/DeviceSession")] 
public class DeviceSession : IDeviceSession 
{ 
public DeviceSession() 
{ 
} 

public DeviceSession(string session, string loginID, string serial) 
{ 
    Session = session; 
    LoginID = loginID; 
    Serial = serial; 
} 

string _session; 

[DataMember(Order = 0)] 
public string Session 
{ 
    get { return _session; } 
    set { _session = value; } 
} 

string _LoginID; 

[DataMember(Order = 1)] 
public string LoginID 
{ 
    get { return _LoginID; } 
    set { _LoginID = value; } 
} 

string _serial; 

[DataMember(Order = 2)] 
public string Serial 
{ 
    get { return _serial; } 
    set { _serial = value; } 
} 

}

回答

3

你在每個例子中究竟做了略微不同的事情。在第一個示例(登錄)中,您在URL中發佈變量,在第二個帖子中發佈正文。

需要注意的是,您需要在第二個示例中發佈的JSON是字符串json,而不是包含字符串的對象的json。這意味着json對類型字符串不是有效的。

我推薦的是將它包裝在一個對象中,或者像第一個例子中那樣在查詢字符串上調用它。

例如,你可以使用

[WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
public WRModel.Entities.Session Login(LoginDetails details) 

在哪裏登錄信息是包含DEVICEID密碼SERIALNUMBER

+0

我跟着你的一類,但我仍然得到同樣的錯誤。看我的編輯。謝謝 – fiberOptics 2012-02-17 09:14:26

+0

Logut方法看起來像這樣:「public bool Logout(WRModel.Entities.DeviceSession deviceSession)」 – fiberOptics 2012-02-17 09:16:23

+0

嘗試使用數據協定serialiser手動將測試對象序列化爲JSON,然後查看您得到的內容,然後發佈。還要確保你的頭文件中設置了json的內容類型,即內容類型:application/json – 2012-02-17 09:21:15