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

uriDium
  • 13,110
  • 20
  • 78
  • 138
  • How is the request json looking. See in your browser debugger console, and post some details here. – Chandermani Nov 04 '13 at 07:38
  • According to HTTP semantics, DELETE should delete the resource under consideration. The url points to the resource location and hence does not require any payload. What you are trying to do is not strictly adhering to the usage of DELETE. – Chandermani Nov 04 '13 at 08:51
  • @Chandermani Someone else suggested that I use post rather. This will not be very restful I guess. Should we be doing the deletes one at a time? Even though there may be 100 of them? – uriDium Nov 04 '13 at 11:55

2 Answers2

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?

Community
  • 1
  • 1
freshbm
  • 5,540
  • 5
  • 46
  • 75
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.

Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
  • Sorry. It is correct in my code. I just typed it straight into the text editor here. It is calling the method. The ids parameter is just null. So I think the route matching is working. – uriDium Nov 04 '13 at 07:29