4

我正在使用带有 AngularJS 资源模块的 servicestack。问题是,当我调用服务的 query() 方法来请求分页列表时,我想发送一个包含总行数的自定义响应标头。

我希望 http 响应看起来像这样:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-Total-Rows: 14
X-Powered-By: ServiceStack/3,958 Win32NT/.NET
Content-Length: 831

[{"id":11,"content":"Item 10","dueDate":"2013-10-17T00:00:00.0000000","priority":9},
{"id":13,"content":"Item 12","dueDate":"2013-06-16T00:00:00.0000000","priority":9},
{"id":20,"content":"Item 19","dueDate":"2013-04-06T00:00:00.0000000","priority":9},
{"id":32,"content":"Item 31","dueDate":"2013-01-21T00:00:00.0000000","priority":9},
{"id":42,"content":"Item 41","dueDate":"2013-05-16T00:00:00.0000000","priority":9},
{"id":19,"content":"Item 18","dueDate":"2013-07-14T00:00:00.0000000","priority":8},
{"id":15,"content":"Item 14","dueDate":"2013-03-06T00:00:00.0000000","priority":7},
{"id":12,"content":"Item 11","dueDate":"2013-02-23T00:00:00.0000000","priority":4},
{"id":18,"content":"Item 17","dueDate":"2013-10-21T00:00:00.0000000","priority":3},
{"id":14,"content":"Item 13","dueDate":"2013-01-11T00:00:00.0000000","priority":2}]

目前,为此,我使用以下 DTO:

[Route("/todos", Verbs = "GET")]
public class Todos : IReturn<List<Todo>> 
{
    [DataMember(Name = "q")] public string Query { get; set; }
    [DataMember(Name = "sort")] public string Sort { get; set; }
    [DataMember(Name = "desc")] public bool IsDesc { get; set; }
    [DataMember(Name = "limit")] public int? Limit { get; set; }
    [DataMember(Name = "offset")] public int Offset { get; set; }
}

[Route("/todos", Verbs = "POST")]
[Route("/todos/{Id}", Verbs = "GET,PUT,DELETE")]
public class Todo : IReturn<Todo>
{
    public int Id { get; set; }
    public string Content { get; set; }
    public DateTime? DueDate { get; set; }
    public int? Priority { get; set; }
}

在服务中:

public object Get(Todos request)
{
    long count;
    var items = _repository.GetAll(request.Query, request.Sort, request.IsDesc, request.Limit, request.Offset, out count);
    Response.AddHeader("X-Total-Rows", count.ToString(CultureInfo.InvariantCulture));
    return items.Select(Mapper.Map<TodoItem, Todo>).ToList();
}

我想在 servicestack 中做的是在 ResponseDTO 中使用自定义属性来指示我希望在响应标头中序列化某些属性,并在响应正文中序列化其他属性。响应 DTO 可能如下所示:

public class TodosResponse
{
    [Header(Name = "X-Total-Rows")] public int TotalRows { get; set; }
    [Body] public List<Todo> Todos { get; set; }
}

该服务将创建如下响应:

public object Get(Todos request)
{
    long count;
    var items = _repository.GetAll(request.Query, request.Sort, request.IsDesc, request.Limit, request.Offset, out count);
    return new TodosResponse
           {
               TotalRows = count,
               Todos = items.Select(Mapper.Map<TodoItem, Todo>).ToList()
           };
}

当然,HTTP 响应看起来与上面完全相同。

所以我的问题是:是否可以使用自定义属性来实现这一点?

4

1 回答 1

4

实现您想要的更简单/更整洁的方法是返回 HttpResult 并在其中定义标题。

public object Get(Todos request)
{
    long count;
    var items = _repository.GetAll(request.Query, request.Sort, request.IsDesc, request.Limit, request.Offset, out count);
    var response = items.Select(Mapper.Map<TodoItem, Todo>).ToList();

    return new HttpResult(response, HttpStatusCode.OK)
            {
                Headers =
                        {
                            {"X-Total-Rows", count}
                        }
            };
}

这样可以避免使用元数据污染您的响应对象

于 2013-09-03T08:44:08.620 回答