CallumVass
CallumVass

Reputation: 11448

ServiceStack: Pass an array to a Service

I'm having an issue when I pass an 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 which is made:

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

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

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

One Solution I've done is the following, which seems a bit hacky? I thought the point of specifying it in my Request Object is that I get a strongly typed Request Object?

var equipmentIds = Request
                       .QueryString["EquipmentIds"]
                       .Split(',')
                       .Select(int.Parse)
                       .ToList();

Upvotes: 4

Views: 1688

Answers (2)

mythz
mythz

Reputation: 143284

This works when you use the custom route, e.g:

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

and call it via the User Defined route, e.g:

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

Support for this has also been added on Predefined Routes in this commit which will be available from v4.0.24+ that's now available on MyGet.

So your existing request now works as well, e.g:

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

Upvotes: 5

Jed I
Jed I

Reputation: 1018

Bind the request object to the int array like

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

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

Upvotes: 1

Related Questions