ServiceStack: pass array to service

I had a problem passing the array to my service, it only recognizes the first value in the array:

Here is my request object:

[Route("/dashboard", "GET")]
public class DashboardRequest : IReturn<Dashboard>
{
    public int[] EquipmentIds { get; set; }
}

Here is the request that is made:

http://localhost:9090/json/reply/DashboardRequest?EquipmentIds=1&EquipmentIds=2

But when I observe an array in my service, it contains only one value, which 1.

public object Get(DashboardRequest request)
{
    // request.EquipmentIds.Length == 1;
    // request.EquipmentIds[0] == 1;
}

One of the decisions I made is the following, which seems a bit hacky? I thought it was pointed out in my request object that I get a strongly typed request object?

var equipmentIds = Request
                       .QueryString["EquipmentIds"]
                       .Split(',')
                       .Select(int.Parse)
                       .ToList();
+4
source share
2 answers

This works when you use a custom route, for example:

[Route("/dashboard", "GET")]
public class DashboardRequest : IReturn<Dashboard>
{
    public int[] EquipmentIds { get; set; }
}

and call it using a user-defined route, for example:

http://localhost:9090/dashboard?EquipmentIds=1&EquipmentIds=2

​​ this commit, v4.0.24 +, MyGet.

, , :

http://localhost:9090/json/reply/DashboardRequest?EquipmentIds=1&EquipmentIds=2
+5

int,

[Route("/dashboard/{EquipmentIds}", "GET")]
public class DashboardRequest : IReturn<Dashboard>
{
    public int[] EquipmentIds { get; set; }
}

 http://localhost:9090/dashboard/1,2
+1

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


All Articles