【发布时间】:2013-11-14 19:25:10
【问题描述】:
我正在尝试使用 $http 向 ASP.Net Web API 控制器方法发布要从服务中删除的整数数组。我都试过了
[HttpDelete]
public HttpResponseMessage DeleteMany(int[] ids) { }
[HttpDelete]
public HttpResponseMessage DeleteMany(DeleteManyDto dto) { }
public class DeleteManyDto
{
public int[] Ids {get;set;}
}
我一直将参数设为空。我在 Angular 服务中尝试了几种变体。这是一个例子
this.deleteMany = function(ids) {
// ids is an int array. e.g. [1,4,6]
return $http.delete('api/Foo/DeleteMany', { toDelete: ids }).then(function(result) {
return result.status;
});
};
有人知道我可能会错过什么吗?
更新:拼写错误。包含来自 HTTP 请求的调试。
Request URL:http://localhost:54827/api/Foo/DeleteMany
Request Method:DELETE
Status Code:500 Internal Server Error
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Cookie:__RequestVerificationToken=blabla
Host:localhost:54827
Origin:http://localhost:54827
Pragma:no-cache
Referer:http://localhost:54827/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Response Headersview source
上面的代码没有产生任何请求负载。当我进行更新时,我会看到请求有效负载。示例
Request Payloadview parsed
{"id":4,"name":"zzzz", "bar": 2}
值得我使用 Angular 1.2.rc3
【问题讨论】:
-
请求 json 的外观如何。在您的浏览器调试器控制台中查看,并在此处发布一些详细信息。
-
根据 HTTP 语义,DELETE 应该删除正在考虑的资源。 url 指向资源位置,因此不需要任何有效负载。您正在尝试做的不是严格遵守 DELETE 的用法。
-
@Chandermani 其他人建议我使用 post。我想这不会很平静。我们应该一次删除一个吗?即使可能有 100 个?
标签: asp.net-mvc-4 angularjs asp.net-web-api