5

My webAPI solution currently works if I send a single id to the delete endpoint:

DELETE /api/object/1

With:

    [HttpDelete]
    public HttpResponseMessage DeleteFolder(int id)
    {
    // Do stuff
    }

In my client application I have a UI that allows for multiple deletes - right now it's just calling this endpoint in a loop for each one of the ids selected, which isn't super performant. I'd like to be able to send an array of Ids to the Delete method in this case... how can this be achieved?

SB2055
  • 12,272
  • 32
  • 97
  • 202
  • This might help you: http://stackoverflow.com/questions/9981330/how-to-pass-an-array-to-integers-to-a-asp-net-web-api-rest-service – Claudio Redi Jul 07 '13 at 20:42

2 Answers2

5
[HttpDelete]
public HttpResponseMessage Folder([FromUri] int[] ids)
{
     //logic
}

api call

DELETE /api/Folder?ids=1&ids=2
ckross01
  • 1,681
  • 2
  • 17
  • 26
5
[HttpDelete]
public HttpResponseMessage DeleteFolder(int[] ids)
{
    // Do stuff
}

and then you could send the following HTTP request:

DELETE /api/somecontroller HTTP/1.1
Accept: application/json
Content-Length: 7
Content-Type: application/json
Host: localhost:52996
Connection: close

[1,2,3]
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Thanks Darin. Are you sticking the array into the body here? – SB2055 Jul 07 '13 at 21:18
  • 3
    Unfortunately the HTTP spec says DELETE bodies are meaningless so you really would need to put the id list as a URI param. – Darrel Miller Jul 08 '13 at 01:51
  • OK - that's why I was asking. I've been looking for an example of sticking data into a URI param with AJAX but I'm coming up short because I don't know what to look for. Would you mind pointing me in the right direction? – SB2055 Jul 09 '13 at 03:31
  • @DarrelMiller forgot to ping you above – SB2055 Jul 09 '13 at 14:00
  • @SB2055 I would just use id=2,3,4. However, I don't really use the web API model binding stuff so, I'm not sure how to get it to magically transform into an array. I'd just pull it as a string from RequestURI.ParseQueryString() and do String.Split in the controller action. – Darrel Miller Jul 09 '13 at 15:15