Pass Array into ASP.NET Core Route Query String
Solution 1
In the end, I just passed in a single delimited string, then used string.Split to separate on the server side. Not the prettiest solution, but it works. Until someone comes up with a better answer, this is all I got. I should reiterate that I'm using .NET Core, and these query strings are framework specific.
Update: An (arguable) benefit of this approach is that you pass the values together (e.g. arr=value1,value2) instead of repeating the key (arr=value1&arr=value2).
Solution 2
Use a parameter name in the query string. If you have an action:
public void DoSomething(string[] values)
Then use values
in the query string to pass an array to a server:
?values=this&values=that
Solution 3
Delimited string is not the standard. Think also about the client if you support swagger or other generators.
For those who wonder about .net core 2.1 bug which receives an empty list, the work around is here: https://github.com/aspnet/Mvc/issues/7712#issuecomment-397003420
It needs a name parameter on FromQuery
[FromQuery(Name = "employeeNumbers")] List<string> employeeNumbers
Solution 4
I have found a solution. For example, if you have a query like this:
http://www.sitename.com/route?arr[]=this&arr[]=that
You must define in parameter as [FromQuery(Name = "arr[]")]
. The name of parameter must include square brackets. As result we can see:
public void DoSomething([FromQuery(Name = "arr[]")] string[] arr)
Solution 5
I had to do something similar to this, but instead of strings, i used a list of long to pass some id for a search. Using a multiple select option, the chosen values are sent to the method (via get) like this:
[HttpGet("[action]")]
public IActionResult Search(List<long> idsSelected)
{
///do stuff here
}
I also use Route("[controller]")
before the class declaration. Works just fine, but the list of items is broken into multiple parameters in the url, as shown below.
http://localhost:5000/Search/idsSelected=1&idsSelected=2
user3685285
Updated on October 27, 2020Comments
-
user3685285 over 3 years
I want to do this, but I want to also be able to pass in arrays into the query string. I've tried things like:
http://www.sitename.com/route?arr[]=this&arr[]=that http://www.sitename.com/route?arr[]=this&that http://www.sitename.com/route?arr[0]=this&arr[1]=that http://www.sitename.com/route?arr0=this&arr1=that http://www.sitename.com/route?arr=this&arr=that
And my route in the C# code looks like this:
[Route("route")] [HttpGet] public void DoSomething(string[] values) { // ... }
But in all of these cases, values is always null when it gets to the C# code. What do I need my query string to be to pass an array of strings?
-
user3685285 about 7 yearsThat's in the list of things I tried. It didn't work for me.
-
Ilya Chumakov about 7 years@user3685285, I checked it and it works as described. Could you pass a single value to the action like
Foo(string value)
? -
Sinaesthetic about 6 yearsThis is how I knew how to do it, but it did not work for me either.
Unexpected end of Stream, the content may have already been read by another component
-
Ilya Chumakov about 6 years@Sinaesthetic how can a stream reading be related to a model binding?
-
Sinaesthetic about 6 yearsI mean... everything is a stream coming over the wire, so it's probably happening during binding. I don't know; that's just the message that's coming out. If I use a single value with
string
instead ofstring[]
it is fine -
keuleJ over 5 yearsAre you missing the [FromUri] attribute?
-
cah1r over 5 years@IlyaChumakov Did you test this on .NET Core ? I mean I have it working on .NET 4.6.2 WebApi but can't seem to get it working in Core 2.1 Version
-
Simon over 5 yearsIn your example, you had arr=1&arr=2 but your variable name is values, so it should surely be values=1&values=2 The name must match.
-
student18 over 5 yearsIs this a bad practise to use a named parameter to use complex types with
[FromQuery]
attribute? -
Adel Tabareh over 5 yearsArray and list behave similarly in this context. But even though this answer is out of the context of current question, I would say, no it's not a bad practice. you can even have complex types like Person and feed it's properties via FromQuery: Look at this for more info: docs.microsoft.com/en-us/aspnet/web-api/overview/…
-
Adel Tabareh over 5 yearsThink about the tools that generate client for your api. I would prefer to see an array of ids as parameter to the client instead of an string parameter which is not self describing.
-
user3685285 over 5 years@MohsenTabareh That's what I was asking. But no one could come up with a way that worked for me. So I just had to do the cheap hack. See the comments in Ilya's answer. Read the context before downvoting please...
-
Adel Tabareh over 5 yearsAnd have you tried the solutions that I & @abatishchev provided?
-
user3685285 over 5 yearsWell I'm not even working at that company anymore. I asked this question more than a year ago. Not sure if there was any updates to .NET Core that addressed this.
-
Machado over 5 yearsI don't have any idea why, but this worked like a charm, thank you so much! Before naming my parameter like your answers says I was getting a 415 HTTP error, now everything is 200.
-
Cory-G over 5 yearsThis answer is solving the wrong problem/ is out of date. You don't need the name param if you match the param name to the values passed in, as other answers point out.
-
Ilya Chumakov about 5 years@MateuszMigała yes, it worked on 1.1 but there is a bug on Core 2.1 as described in the answer stackoverflow.com/a/52222184/5112433. The bug was fixed in 2.2.
-
GuyB almost 5 yearsMy querystring was in
?arr[]=this&arr[]=that
format, and the only way I could get it to work was by including the square brackets in the FromQuery name:[FromQuery(Name = "employeeNumbers[]")] List<string> employeeNumbers
. I've no idea whether that is valid or not. -
Michael Freidgeim over 3 yearsYou can remove [] from both query and parameter declaration, e.g. route?arr=this&arr=that should work
-
Michael Freidgeim over 3 yearsThe solution is very similar to the accepted answer stackoverflow.com/a/43474678/52277 and user3685285’s Split is more simple than JsonConvert.DeserializeObject<List<string>>
-
Machado over 3 yearsThanks, it's fixed now.
-
Justin Harris almost 3 yearsIt's annoying to have to do this but in the end, I couldn't find an easier solution when using deep objects.
-
Roi Shabtai almost 2 years@user3685285 This is not an answer but workaround