简体   繁体   中英

Pass Array into ASP.NET Core Route Query String

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?

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

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

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)

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 (eg arr=value1,value2) instead of repeating the key (arr=value1&arr=value2).

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

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

I found two problems in your question:

  1. Your query has parameters named arr while you Contrller's Action has values .
  2. I don't know why, but you gotta name your parameter ( as answered here ) so the Asp .NET ModelBinder can work as expected. Like this:
public void DoSomething([FromQuery(Name = "values")] string[] values)

After doing that, everything should work as expected.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM