4
//DTO
public class SampleDto : IReturn<SampleDto>
{
  public int Id { get; set; }
  public string Description { get; set; }
}
public class ListSampleDto : IReturn<List<SampleDto>>
{
}
//Service
public class SampleService : Service
{
  public object Get(ListSampleDto request)
  {
    List<SampleDto> res = new List<SampleDto>();
    res.Add(new SampleDto() { Id = 1, Description = "first" });
    res.Add(new SampleDto() { Id = 2, Description = "second" });
    res.Add(new SampleDto() { Id = 3, Description = "third" });
    return res;
  }
}
//Client
string ListeningOn = ServiceStack.Configuration.ConfigUtils.GetAppSetting("ListeningOn");
JsonServiceClient jsc = new JsonServiceClient(ListeningOn);
// How to tell the service only to deliver the objects where Description inludes the letter "i"
List<SampleDto> ks = jsc.Get(new ListSampleDto()); 

我不知道如何从 JsonServiceClient 向服务发送过滤条件(例如,仅获取 Description 包含字母“i”的对象)。

4

1 回答 1

2

在这种情况下,您通常使用您在服务器端评估的属性扩展您的输入 Dto(在本例中为ListSampleDto)以提供正确的响应:

// Request Dto:
public class ListSampleDto
{
  public string Filter { get; set; }
}
...
// Service implementation:
public object Get(ListSampleDto request)
{
  List<SampleDto> res = new List<SampleDto>();
  res.Add(new SampleDto() { Id = 1, Description = "first" });
  res.Add(new SampleDto() { Id = 2, Description = "second" });
  res.Add(new SampleDto() { Id = 3, Description = "third" });
  if (!string.IsNullOrEmpty(request.Filter)) {
    res = res.Where(r => r.Description.StartsWith(request.Filter)).ToList()
  }
  return res;
}
...
// Client call:
List<SampleDto> ks = jsc.Get(new ListSampleDto { Filter = "i" }); 
于 2013-03-25T16:39:55.213 回答