【问题标题】:DeleteAsync return status 204 but not deleting the itemDeleteAsync 返回状态 204 但不删除项目
【发布时间】:2018-05-10 06:41:21
【问题描述】:

我是 API 调用的新手,我编写了 Create 和 Update 方法,它们工作正常,但 DeleteAsync 调用返回状态 204 但不删除项目。请求参数类似于"FieldsOfBusiness/13158?Code=%27555%27"。在 Postman 中用撇号替换 %27 时效果很好

public async Task<bool> DeleteAsync<T>(object id, NameValueCollection parameters)
    {
        var queryString = ToQueryString(parameters);
        HttpResponseMessage response = await Client.DeleteAsync($"{GetName<T>()}/{id}{queryString}")
            .ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
        var a =  await response.Content.ReadAsAsync<T>();
        return response.IsSuccessStatusCode;
    }     


protected string ToQueryString(NameValueCollection nvc)
    {
        if (nvc == null || nvc.Count < 1)
            return string.Empty;

        var array = (from key in nvc.AllKeys
                     from value in nvc.GetValues(key)
                     select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value)))
            .ToArray();
        return "?" + string.Join("&", array);
    }  

【问题讨论】:

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


【解决方案1】:

我猜您无法控制服务器或其响应方式。正如在 cmets 204 中所指出的,即使实际上没有删除任何内容,成功的 DELETE 请求也是典型的正常响应。您想知道响应中是否有任何内容可以告诉您文件是否已被删除。您可以检查 HttpResponseMessage.ReasonPhrase 看看是否不同。

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 2023-04-01
    • 2016-12-25
    • 1970-01-01
    • 2016-10-13
    • 2017-06-09
    • 1970-01-01
    • 2012-09-10
    • 2012-02-28
    相关资源
    最近更新 更多