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?

:)

2
  • Duplicate of stackoverflow.com/questions/10093514/… Commented Dec 3, 2014 at 9:59
  • 1
    Have you considered using the built-in HTTP batching support Commented Dec 6, 2014 at 22:47

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.