Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
How is the request json looking. See in your browser debugger console, and post some details here. –  Chandermani Nov 4 '13 at 7:38
 
@Chandermani Updated –  uriDium Nov 4 '13 at 8:36
 
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 4 '13 at 8: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 4 '13 at 11:55
add comment

2 Answers

up vote 1 down vote accepted

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:

Pretty coding to send body with DELETE request in Angular.js

share|improve this answer
 
Tried both. Neither worked. It seems that angular is not adding the request payload. –  uriDium Nov 4 '13 at 8:37
 
I ended up using the HTTP post with the int array. –  uriDium Nov 6 '13 at 8:52
add comment

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.

share|improve this answer
 
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 4 '13 at 7:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.