排除WebAPI响应

时间:2017-01-24 09:54:09

标签: c# asp.net-web-api

public class Order
{
    public decimal Id { get; set; }
    public decimal customerId { get; set; }
    public string payment_method { get; set; }
    public string payment_status { get; set; }
    public double total_price { get; set; }
    public string date { get; set; }
    public List<OrderDetail> OrderDetails { get; set; }
    public List<Address> OrderAddresses { get; set; }
}

public class OrderDetail
{
    public decimal product_id { get; set; }
    public string product_name { get; set; }
    public double price { get; set; }
    public double quantity { get; set; }
}

public class Address
{
    public string type { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string state_province { get; set; }
    public string postal_code { get; set; }
    public string country { get; set; }
    public string telephone { get; set; }
    public string fax { get; set; }
}

这是我的模型我的一些API。使用orderAddorderDetail等整个模型。但是,对于订单列表,我想要Order模型中的少数字段,例如 Idtotal_pricedate并在Web API响应中排除其余字段和列表。

下面是我的回复代码

[ResponseType(typeof(List<Order>))]
[HttpGet, Route("api/Orders/getOrderList")]
public IHttpActionResult getOrderLists(decimal customerId, int limit = 3)
{
    List<Order> Model = new List<Order>();
    var orders = db.OrderMasters.Where(p => p.CustomerId == customerId).Take(limit);
    foreach (var o in orders)
    {
        Order O = new Order();
        O.Id = o.OrderId;
        O.date = o.PurchasedOn;
        O.total_price = o.Total;
        Model.Add(O);
    }

    return Ok(Model);
}

0 个答案:

没有答案