gpt4 book ai didi

ServiceStack:将数组传递给服务

转载 作者:行者123 更新时间:2023-12-05 00:25:20 25 4
gpt4 key购买 nike

当我将数组传递给我的服务时遇到问题,它只识别数组中的第一个值:

这是我的请求对象:

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

这是提出的请求:
http://localhost:9090/json/reply/DashboardRequest?EquipmentIds=1&EquipmentIds=2

但是当我观察我的服务中的数组时,它只包含一个值,即 1 .
public object Get(DashboardRequest request)
{
// request.EquipmentIds.Length == 1;
// request.EquipmentIds[0] == 1;
}

我已经完成的一个解决方案如下,这似乎有点hacky?我认为在我的请求对象中指定它的重点是我得到一个强类型的请求对象?
var equipmentIds = Request
.QueryString["EquipmentIds"]
.Split(',')
.Select(int.Parse)
.ToList();

最佳答案

这在您使用自定义路由时有效,例如:

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

并通过用户定义的路由调用它,例如:
http://localhost:9090/dashboard?EquipmentIds=1&EquipmentIds=2

Predefined Routes 上也添加了对此的支持在 this commit可从 获得v4.0.24+ 现在是 available on MyGet .

因此,您现有的请求现在也可以正常工作,例如:
http://localhost:9090/json/reply/DashboardRequest?EquipmentIds=1&EquipmentIds=2

关于ServiceStack:将数组传递给服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24777565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com