1

当我运行这段代码时,数据没有显示,我得到了一个空白屏幕。如果我单击"in other formats: json"它会返回"{ }"空白数据。

但是,它实际上有数据。如果我单击"in other formats: xml"它会返回完整数据。

那么,为什么我的 XML 工作正常,但我的 HTML 和 JSON 显示空白屏幕?

[Route("customers", "GET")]
[Route("customers/{Id}", "GET")]
[Route("customers/page/{Page}", "GET")]
[Route("customers/page/{Page}/size/{Size}", "GET")]
[Route("customers/{Id}/orders", "GET")]
[Route("customers/{Id}/orders/page/{Page}", "GET")]
[Route("customers/{Id}/orders/page/{Page}/size/{Size}", "GET")]
public class Customers {
    public Customers() { Page = 1; Size = 20; }
    public string Id { get; set; }
    public int Page { get; set; }
    public int Size { get; set; }
}
public class CustomersResponse {
    public IList<Customer> Customers;
    public int Pages;
}
public class CustomerDetailResponse {
    public Customer Customer;
    public IList<Order> Orders;
    public int Pages;
}
public class CustomersService : BaseService {
    public ICustomersManager CustomersManager { get; set; }
    public IOrdersManager OrdersManager { get; set; }
    public IValidator<Customers> Validator { get; set; }  
    public object Get(Customers req) {
        var result = Validator.Validate(req);
        if(!result.IsValid) return result.ToResponseDto();
        if (req.Id == null) {
            return new CustomersResponse {
                Customers = CustomersManager.GetCustomers(req), 
                Pages = CustomersManager.GetCustomersPageCount(req) };
        }
        else {
            return new CustomerDetailResponse {
                Customer = CustomersManager.GetCustomer(req), 
                Orders = OrdersManager.GetOrders(req), 
                Pages = OrdersManager.GetOrdersPageCount(req) };
        }
    }
}
4

1 回答 1

1

默认情况下, ServiceStack 的文本序列化器仅序列化公共属性,您的 DTO 具有字段和属性的混合。要么将所有 DTO 更改为公共属性,要么将其添加到您的AppHost.Configure()

ServiceStack.Text.JsConfig.IncludePublicFields = true;
于 2013-03-08T09:25:23.040 回答