0

Using WebClient do send an array to an ApiController via query string I get the error 400.

The api method looks like

public IHttpActionResult List([FromUri] Model model)

In the Model class I have

public int[] Ids { get; set; }

In the client side the code looks like:

webClient.QueryString.Add("ids", "1");
webClient.QueryString.Add("ids", "2");
...
await webClient.DownloadStringTaskAsync(url);

If I send just one "ids" parameter the code works fine, but not with two or more.

I found that the client creates the url like "url?ids=1,2" instead "url?ids=1&ids=2".

Is there some configuration I missed?

Max Bündchen
  • 1,283
  • 17
  • 38

1 Answers1

1

WebClient will automatically turn multiple values with the same key into a comma separated string. You can change this behavior, see: How to build WebClient querystring with duplicate keys?

I would recommend using HttpClient instead of WebClient though.

Rudey
  • 4,717
  • 4
  • 42
  • 84