我想从我的代码发布数据,但没有更改数据。当我调试代码时它得到了200,这意味着代码是好的。我尝试在fiddler中测试并且数据已成功更改。我想知道哪里错了? 这是我的服务,包括网址
public static async Task<string> PostOrderAsync(MachinePostModel Order)
{
try
{
var client = new HttpClient();
string data = JsonConvert.SerializeObject(Order);
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://94.205.253.150:2016/api/JobOrder", content);
return data;
}
catch
{
return null;
}
}
这是我发送订单对象的步骤
private async Task<bool> AcceptPostOrder()
{
MachinePostModel OrderDetails = new MachinePostModel();
try
{
OrderDetails.ID = 1163;
OrderDetails.joStatus = "should to wait";
OrderDetails.remarks = "hello remarks";
var Client = await Services.MachineTestPostService.PostOrderAsync(OrderDetails);
return true;
}
catch (Exception exc)
{
await MachineTestOrderView.machineobj.DisplayAlert("Alert", exc.Message, "OK");
return false;
}
}
最后我的模特
public class MachinePostModel
{
public int ID { get; set; }
public string remarks { get; set; }
public string joStatus { get; set; }
}
获取链接以检查数据是否已更改
http://94.205.253.150:2016/api/JobOrder/Get?regId=1&driverID=35&status=All%20Status
答案 0 :(得分:0)
您的代码中存在一些错误。首先,使用HttpPost
属性修饰您的操作,并为发送的复杂对象添加参数。
[HttpPost]
private async Task<bool> AcceptPostOrder(MachinePostModel Order)
{
MachinePostModel OrderDetails = new MachinePostModel();
try
{
OrderDetails.ID = 1163;
OrderDetails.joStatus = "should to wait";
OrderDetails.remarks = "hello remarks";
var Client = await Services.MachineTestPostService.PostOrderAsync(OrderDetails);
return true;
}
catch (Exception exc)
{
await MachineTestOrderView.machineobj.DisplayAlert("Alert", exc.Message, "OK");
return false;
}
}
并且,没有路由定义为api/JobOrder
,因此我假设您尝试发布数据AcceptPostOrder
方法。更改请求端点;
var response = await client.PostAsync("http://94.205.253.150:2016/api/AcceptPostOrder", content);