c# - 从网络服务中删除数据不显示任何内容?赛马林
问题描述
我一直在尝试使用“DeleteAsync”删除数据,它没有显示任何内容,也没有显示任何错误,当我点击删除按钮时没有任何反应。虽然事情对我来说似乎很好,但是你们帮助我错过了什么?
这是代码
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");
}
}
解决方案
您的 Web API url 似乎是错误的,因为它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.
}
}
推荐阅读
- netsuite - NetSuite - 向标准交易表单添加功能?
- java - 如何在 react-dnd-treeview 库上使用 Selenium 测试拖放
- elasticsearch - 通过 Logstash 在大型数据集上加载 Elasticsearch 运行非常缓慢
- angular - Angular 5 中的单元测试模型绑定与 Jasmine
- node.js - 在 Node.js 服务器中支持 HTTPS 或需要 HTTPS 的反向代理
- php - 如何在 Windows 上查看 XAMPP 的版本?
- node.js - Node.js 和管理 MongoDB 数据库
- angular - ng : 一个元素上不能有多个模板绑定。仅使用一个名为 template 或以 * 为前缀的属性
- java - 如何在一个条件下访问或使用另一个类的变量
- python - Numpy dot 操作未使用所有 cpu 内核