0

I am trying to post an array of integers to delete from a service using $http to ASP.Net Web API controller method. I have tried both

[HttpDelete]
public HttpResponseMessage DeleteMany(int[] ids) { }

[HttpDelete]
public HttpResponseMessage DeleteMany(DeleteManyDto dto) { }

public class DeleteManyDto
{
    public int[] Ids {get;set;}
}

I keep getting the params as null. I have tried a couple of variations in the Angular service. Here is an example

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;
    });
};

Anyone know what I could be missing?

UPDATE: Spelling mistake. Included debug from HTTP request.

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

The code above is not producing any request payload. When I do an update I see the request payload. Example

Request Payloadview parsed
{"id":4,"name":"zzzz", "bar": 2}

For what it is worth I am using Angular 1.2.rc3

4

2 回答 2

2

Try changing your controller method signature to this:

[HttpDelete]
public HttpResponseMessage DeleteMany(int[] toDelete) { }

or if you using your ViewModel than change delete call to this:

return $http.delete('api/Foo/DeleteMany', { Ids: ids }).then(function(result) {
        return result.status;
    });

Edit:

But I think that real problem is in $http.delete call. You can't send body with DELETE verb in HTTP, and because MVC binds data from message body or url your array is not binded. Try implement DeleteMany with POST request.

http://docs.angularjs.org/api/ng.$http#methods_delete

You see that in documentation delete method doesn't have data param in its definition.

Or you can try this approach, where you add your data to message header:

What is a clean way to send a body with DELETE request?

于 2013-11-04T08:21:26.173 回答
1

You have a spelling error in your API:

public HttpResponseMessage DeleteManay(int[] ids) { }

should be:

public HttpResponseMessage DeleteMany(int[] ids) { }

Update:

Appears it was a spelling mistake in the question.

于 2013-11-04T07:26:40.983 回答