我在Xamarin上使用RestSharp與WebAPI使用POST進行通信。我可以發送一些信息並得到迴應,但結果並不符合我的預期。Xamarin RestSharp:將Json對象發送到Azure Web API並返回空結果
這是我在Xamarin上的代碼。
var request = new RestSharp.RestRequest ("api/device/stats", RestSharp.Method.POST);
request.AddHeader ("Content-Type", "application/json; charset=utf-8");
request.RequestFormat = DataFormat.Json;
request.AddBody(new AppUsageInfo {MAC = "ASDF"});
RestSharp.IRestResponse response = client.Execute (request);
var content = response.Content;
在我的WebAPI:
public string Post([FromUri]UsageLogModel usageState)
{
//LogFunction.AddUsageLogs(usageState);
if (usageState.MAC == null)
return "fail";
else
return "success";
}
UsageLogModel是:
public class UsageLogModel
{
public string MAC;
}
不知何故的響應是 「失敗」 的MAC是空的。我抓了我的腦袋,但不知道發生了什麼.-
在其他請求中使用的類型'AppUsageInfo'is從你在你期待一個不同的POST方法'UsageLogModel' – Milen