我在使用POST時未對ServiceStack Json客戶端反序列化我的結果時遇到問題。 get方法在將響應反序列化爲UserCredentials對象時沒有問題,但是當我使用POST方法時,它會返回一個具有所有null屬性的對象。如果我將傳遞給Post方法的類型更改爲字典,我可以看到它獲得了正確的結果,並且我可以看到其他調用在來自Fiddler的http級別成功。ServiceStack json客戶端發佈方法未正確反序列化
public UserCredentials Login(string uname, string pwd)
{
var url = "/login";
//note that I have to send a dictionary because if I sent a Login Object with username and password it erronously sends {Login:} in the raw request body instead of {"Uname":uname, "Password":password}
var login = new Dictionary<string, string>() { { "Uname", uname }, { "Password", pwd } };
var result = client.Post<UserCredentials>(url, login);
return result;
}
這裏的響應(這是從服務器的正確和預期的HTTP響應)
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 14 Mar 2013 12:55:33 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Length: 49
{"Uid":1,"Hash":"SomeHash"}
這裏是UserCredentials類
public class UserCredentials
{
public long Uid;
public string Hash;
}
爲什麼使用字典而不是UserCredentials實例? – kampsj 2013-03-14 14:28:05
我試着發送用戶憑證,但是通過發送{Login:}而不是{Uname:「foo」,Password:「bar」}發生了錯誤。我寧願發送UserCredentials,但我無法讓它工作。有沒有建議的方式來做到這一點? – MonkeyBonkey 2013-03-14 17:56:34
我想你將不得不發佈整個代碼,如果這些答案都沒有解決它。 – kampsj 2013-03-14 22:47:59