我一直在尝试使用“ DeleteAsync”删除数据,它什么也没有显示,也没有错误,当我点击Delete按钮时什么也没发生。 尽管对我来说情况似乎不错,但是你们在我错过的地方帮了忙?
这是代码
private async void Delete(object sender, EventArgs e)
{
private const string weburl = "http://localhost:59850/api/Donate_Table";
var uri = new Uri(string.Format(weburl, txtID.Text));
HttpClient client = new HttpClient();
var result = await client.DeleteAsync(uri);
if (result.IsSuccessStatusCode)
{
await DisplayAlert("Successfully", "your data have been Deleted", "OK");
}
}
答案 0 :(得分:2)
您的Web API网址似乎有误,因为weburl
是使用
private const string weburl = "http://localhost:59850/api/Donate_Table";
var uri = new Uri(string.Format(weburl, txtID.Text));
请注意weburl
中缺少的占位符,但它正在string.Format(weburl, txtID.Text
中使用
由此看来,weburl
可能就是
private const string weburl = "http://localhost:59850/api/Donate_Table/{0}";
以便要删除的资源id
将成为所调用URL的一部分。
通常还建议人们避免重复创建HttpClient
的实例
private static HttpClient client = new HttpClient();
private const string webUrlTempplate = "http://localhost:59850/api/Donate_Table/{0}";
private async void Delete(object sender, EventArgs e) {
var uri = new Uri(string.Format(webUrlTempplate, txtID.Text));
var result = await client.DeleteAsync(uri);
if (result.IsSuccessStatusCode) {
await DisplayAlert("Successfully", "your data have been Deleted", "OK");
} else {
//should have some action for failed requests.
}
}