Reputation: 6586
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?
Upvotes: 63
Views: 124632
Reputation: 10324
I found two problems in your question:
arr
while you Contrller's Action has values
.ModelBinder
can work as expected. Like this:public void DoSomething([FromQuery(Name = "values")] string[] values)
After doing that, everything should work as expected.
Upvotes: 6
Reputation: 6586
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).
Upvotes: 7
Reputation: 681
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)
Upvotes: 43
Reputation: 41
I had the same problem with .NET Core 3, while trying to pass in a string Array. I solved it by passing in the query parameter as a temporary json string. I then deserialized the string to the resulting array using Newtonsoft's Json package
using Newtonsoft.Json;
public IActionResult Get([FromQuery(Name = "array")] string arrayJson)
{
List<string> array = JsonConvert.DeserializeObject<List<string>>(arrayJson);
}
Upvotes: 4
Reputation: 100258
Given:
public ValuesController
{
public IACtionResult Get([FromUri]string[] arr)
{
Return Ok(arr.Length);
}
}
The following request will work:
GET /api/values/?arr[0]=a&arr[1]=b&arr[2]=c
Upvotes: 6
Reputation: 1776
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
Upvotes: 47
Reputation: 25019
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
Upvotes: 62
Reputation: 391
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
Upvotes: 10