首页 > 解决方案 > 从网络服务中删除数据不显示任何内容?赛马林

问题描述

我一直在尝试使用“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");
        }
    }

标签: c#web-servicesxamarinasp.net-web-api

解决方案


您的 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.
    }
}

推荐阅读