3

Using Visual Studio 2012.2, MVC4 web application.

I have requests coming to my ApiController like so:

http://localhost/api/keys?ids[]=1&ids[]=2&ids[]=3

And I was under the impression that the following method should be able to automagically retrieve the values from the ids[] array:

public KeysModel Get(int[] ids){...}

However, when a request like the one above comes in, the value of the "ids" parameter is null.

I have inspected that HttpContext.Current.Request.QueryString has the values for ids, and I could get to them that way, but that makes unit testing harder.

I have also tried using List ids, [FromUri], [FromUri(Name="ids[]")], object ids, and string ids (interesting note... when ids is a string variable, the value within it is "(Collection)"

5
  • 1
    Can you try changing your uri to be like this and try to see if it works: http://localhost/api/keys?ids=1&ids=2&ids=3 and also you should decorate the parameter with a [FromUri] attribute as by default Web API considers complex types to be coming from body Commented Aug 30, 2013 at 18:13
  • cant change the uri. it is coming from ember this way, and the goal is to not make a custom adapter/serializer, and instead bend to the will of ember. Commented Aug 30, 2013 at 18:19
  • Turns out I was failing to use [FromUri] correctly (thought for certain I had... must be a "stared at it too long" issue). Commented Aug 30, 2013 at 18:20
  • Don't use [] in your querystring, I believe that's a PHP specific idiom. Commented Aug 30, 2013 at 18:23
  • Don't use the same key multiple times in your querystring, that's just stupid! Commented Aug 30, 2013 at 18:24

1 Answer 1

5

Turns out:

public KeysModel Get([FromUri]int[] ids){...}

Was the answer after all.

Dunno what I was doing before...

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.