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?

1

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Darin. Are you sticking the array into the body here?
Unfortunately the HTTP spec says DELETE bodies are meaningless so you really would need to put the id list as a URI param.
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?
@DarrelMiller forgot to ping you above
@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.
5
[HttpDelete]
public HttpResponseMessage Folder([FromUri] int[] ids)
{
     //logic
}

api call

DELETE /api/Folder?ids=1&ids=2

3 Comments

FYI...Support for model binding collections from Uri was a problem in MVC 4 Web API, which was fixed later in MVC 5 Web API.
@KiranChalla I'm sure you mean ASP.NET Web API 4 and ASP.NET Web API 5, right? ;-)
@DarrelMiller That's not how they're called.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.