【问题标题】:WCF and RESTful API: remote server returned bad request 400 on DELETE methodWCF 和 RESTful API:远程服务器在 DELETE 方法上返回错误请求 400
【发布时间】:2015-09-29 22:56:19
【问题描述】:

我检查了大量的帖子/教程/视频,但在一种特定情况下仍然无法使用 DELETE 方法获得工作代码:当我尝试按 ID 删除时。

下面的工作示例。我将整个 json 字符串传递给 DELETE 方法,它运行良好(基于它我敢建议 wcf/客户端配置文件中没有错误)。
WCF

[OperationContract]
[WebInvoke(Method = "DELETE",
    ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,
    UriTemplate = "delete"
)]
bool DeleteNews(News news);

客户:

const string WEBSERVICE_URL = "http://localhost:33873/NewsService.svc/delete";
            string jsonData = "{my json string}";
            try
            {
                var webRequest = WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method = "DELETE";
                    webRequest.Timeout = 20000;
                    webRequest.ContentType = "application/json";
                    using (Stream s = webRequest.GetRequestStream())
                    {
                        using (StreamWriter sw = new StreamWriter(s))
                            sw.Write(jsonData);
                    }

                    using (Stream s = webRequest.GetResponse().GetResponseStream())
                    {    
                    }    
                 }    
             }

现在想演示以下对我不起作用的代码。我想知道我不能按 ID 使用 DELETE。
WCF

[OperationContract]
[WebInvoke(Method = "DELETE",
    UriTemplate = "delete/?id={id}"
)]
bool DeleteNews(int id);

将 URL 放入浏览器,如 http://localhost:33873/NewsService.svc/delete/?id=10 并得到“远程服务器返回错误请求,代码为 400”(意味着客户端或我的请求有问题)。 我也尝试过如下字符串参数:

[OperationContract]
[WebInvoke(Method = "DELETE",
    UriTemplate = "delete/{id}"
)]
bool DeleteNews(string id);

经过这样的转换,URL 看起来像http://localhost:33873/NewsService.svc/delete/10
结果也不成功,同样的错误。

【问题讨论】:

  • “将 URL 放入浏览器”实际上是 GET 请求。您确定要使用 DELETE 方法发送请求吗?

标签: c# asp.net wcf api


【解决方案1】:

您是否在 IISExpress 中启用了“删除”动词

http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-express-faq http://stevemichelotti.com/resolve-404-in-iis-express-for-put-and-delete-verbs/

您也可以使用 Fiddler 手动测试 PUT、DELET 动词

【讨论】:

  • 谢谢!链接非常有用。我检查了 PUT 和 DELET 动词 - 两者都在 IISExpress 中启用。
  • 你可以使用fiddler中的compose标签来测试DELETE动词intstrings.com/ramivemula/articles/…
【解决方案2】:

问题已解决。 我已经尝试了几乎所有我发现与这个问题有关的东西。根据错误代码,我 100% 确定这是我发送到服务器的错误请求/网址。
一旦我在配置文件中更改了服务器行为以查看服务器端是否有任何异常,我就找到了导致问题的原因。

  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>

我将includeExceptionDetailInFaults 转为true 并得到以下详细信息:

根据描述,由于错误的 LINQ 查询,我的服务器无法处理请求。我的 DELETE 方法的实现如下:

public bool DeleteNews(string id)
{
    using (EF.ServiceDBEntities context = new EF.ServiceDBEntities())
    {
        var n = context.News.FirstOrDefault(x => x.NewsID == int.Parse(id));
        if (n != null)
        {
            context.News.Remove(n);
            context.SaveChanges();
            return true;
        }
    }
    return false;
}

这种情况x =&gt; x.NewsID == int.Parse(id)是不允许的。当我将int.Parse(id) 移出查询问题时,问题就消失了。一些细节here.
即使这样,我还是不明白为什么我的服务器返回错误代码 400(错误请求)?这个异常是由服务器产生的,不是吗?据我所知,代码 500 属于内部服务器错误,我的异常就是一个很好的示例。任何 cmets 都表示赞赏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多