ServiceStack:在DTO中使用属性来设置响应头和响应主体

时间:2013-08-31 12:15:56

标签: angularjs servicestack angular-resource

我正在使用带有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响应看起来与上面完全一样。

所以我的问题是:是否可以使用自定义属性实现此目的?

1 个答案:

答案 0 :(得分: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}
                        }
            };
}

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