Reputation: 15149
Having the following action:
public IHttpActionResult GetStuff(
string like = null,
[FromUri]string[] state = null,
[FromUri]string[] mode = null,
[FromUri]string[] label = null,
)
when I query it as /api/stuff?state=A&state=B
the model binder instantiates state
array with 2 string values and it's kind enough to instantiate empty mode
and label
arrays so I don't need to check for nulls. However, if I query it as /api/stuff?&state=A&state=B
(note extra ampersand) mode
and label
arrays are no longer empty - they contain a single null
elements both. Why?
In my understanding the query strings are equivalent. Is there any way to fix it without writing custom binder?
Upvotes: 3
Views: 1416
Reputation: 20033
The two query strings are not equivalent. Your second query has a second, nameless, parameter.
/api/stuff?state=A&state=B
= query-string with 1 parameter, state
.
/api/stuff?&state=A&state=B
= query-string with 2 parameters, state
and a nameless
one.
?&
translates to a nameless parameter name having a null
value.
The Route parser correctly parses state
. However it detects a second parameter and it doesn't know where to put it. It ends up putting it in both your other parameters.
The Route parser's logic is similar with this:
- I'm looking for state
... oh here is it, let's populate.
- I'm looking for mode
... nothing found but hey there's a nameless one, let's populate with it.
- I'm looking for label
... nothing found but hey there's a nameless one, let's populate with it.
PS: you can send a nameless query-string parameter like this:
http://www.myurl.com/?=value
Upvotes: 3