我在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