ServiceStack使用特定json的Restful请求

时间:2017-04-11 01:16:04

标签: c# servicestack

我需要使用生成此Service stack请求的JSON创建请求:

[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

我试过了:

[Route("/getconsignments/{ConsignmentNumbers}", "POST")]
public class GetConsignments : IReturn<PublishManifestResponse>
{
    public string[] ConsignmentNumbers { get; set; }
}

public class PublishManifestResponse
{   
    [DataMember(Name = "ManifestNumber")]
    public string ManifestNumber { get; set; }
    [DataMember(Name = "ManifestedConnotes")]
    public string[] ManifestedConnotes { get; set; }
}

但它没有产生我需要工作的请求。

此外,

如何填写请求中的json数据?

var request = new List<string>();
request.Add("abcd");
reuqest.Add("wewwq");

var client = new Client(new JsonServiceClient(appSettings.GetString("host")));
var result = client.GetConsignments(request.ToArray());

那样的东西?

1 个答案:

答案 0 :(得分:1)

您可以接受inheriting from a generic List的批量请求,例如:

[Route("/getconsignments/{ConsignmentNumber}", "POST")]
public class GetConsignments : List<string>, IReturn<PublishManifestResponse> 
{
    public string ConsignmentNumber { get; set; }
}

如果您需要使用IReturn<string[]>返回JSON集合。

这将允许您接受概念上类似于:

的请求
POST /getconsignments/1
[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

如果除了请求正文之外还需要发送string[],那么您不应该在路由的/path/info部分中使用ConsignmentNumbers[]之类的复杂类型在QueryString上指定,而您需要从路由中删除,例如:

[Route("/getconsignments", "POST")]
public class GetConsignments : List<string>, IReturn<PublishManifestResponse> 
{
    public string[] ConsignmentNumbers { get; set; }
}

将接受以下请求:

POST /getconsignments?ConsignmentNumbers=1,2,3
[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

但是如果你只需要JSON数组,那么你只需要一个空的Request DTO定义,例如:

[Route("/getconsignments", "POST")]
public class GetConsignments : List<string>, IReturn<PublishManifestResponse> {}

接受以下请求:

POST /getconsignments
[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]