Passing an array to the query string of the main ASP.NET route

I want to do this , but I also want to pass arrays to the query string. I 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 C # code looks like this:

[Route("route")]
[HttpGet]
public void DoSomething(string[] values)
{
    // ...
}

But in all these cases, the values ​​are always zero when they fall into C # code. Why do I need a query string to pass an array of strings?

+11
source share
7 answers

, , string.Split . , . - , , . , .NET Core, .

0

. , .

, .net core 2.1, , : https://github.com/aspnet/Mvc/issues/7712#issuecomment-397003420

FromQuery

[FromQuery(Name = "employeeNumbers")] List<string> employeeNumbers
+13

. :

public void DoSomething(string[] values)

values , :

?values=this&values=that
+9

- , long id . , ( get) :

[HttpGet("[action]")]
public IActionResult Search(List<long> idsSelected)
{
    ///do stuff here
}

Route("[controller]") . , URL-, .

http://localhost:5000/Search/idsSelected=1&idsSelected=2
+4

:

public ValuesController
{
    public IACtionResult Get([FromUri]string[] arr)
    {
        Return Ok(arr.Length);
    }
}

:

GET /api/values/?arr[0]=a&arr[1]=b&arr[2]=c
+2

:

  1. arr Contrller values.
  2. , ( ), Asp.NET ModelBinder . :
public void DoSomething([FromQuery(Name = "values")] string[] values)

.

+2

. , , http://www.sitename.com/route?arr[]=this&arr[]=that. [FromQuery (Name = "arr []")]. "arr []". :

public void DoSomething ([FromQuery (Name = "arr []")] string [] arr)

0
source

Source: https://habr.com/ru/post/1674770/


All Articles