4

In my WebApi I want to have method witch accepts ID list as parameter and deletes them all. So this method looks like code below.

[HttpDelete]
public IHttpActionResult DeleteList([FromBody]IEnumerable<int> templateIds)

I also need Unit Test to be written for this using .NET's HttpClient. So my question is: how can I pass array/list of Ids to WebApi using HttpClient DeleteAsync?

:)

Ohad Horesh
  • 4,340
  • 6
  • 28
  • 45

1 Answers1

5

As far as I am aware sending a message body in a HTTP DELETE is ignored in Web Api. An alternative is to take in the list of Ids from the Uri:

    [HttpDelete] 
    public void DeleteList([FromUri]IEnumerable<int> ids)

then call the url with: http://server/api/ControllerRoute/ids=1&ids=2

To call this from HttpClient, you'd make the DeleteAsync directly because all of the data is in the Url.

You could also break out System.Web.HttpUtility to build the query string in the Url for you, like so:

        var httpClient = new HttpClient();
        var queryString = HttpUtility.ParseQueryString(string.Empty);
        foreach (var id in ids)
        {
            queryString.Add("ids", id.ToString());
        }
        var response = httpClient.DeleteAsync(queryString.ToString());
MartinM
  • 1,736
  • 5
  • 20
  • 33