【发布时间】:2013-09-13 13:48:43
【问题描述】:
我看到这表明我可以根据用户构建不同的视图: different json views for the same entity
但是在 asp web api 中,使用模型类,我不能随便添加新属性。
所以,例如我可能有 uri:
http://host/api/products/id
返回模型:
public class Product{
public string Code { get; set; }
public string Description { get; set; }
}
但出于另一个目的,我想添加更多信息,假设这很昂贵,因为它会加入其他数据来构建模型,或者以非常特定的方式格式化数据:
http://host/api/productsspecial/id
返回模型:
public class ProductSpecial{
public string Code { get; set; }
public string Description { get; set; }
public decimal Price { get; set; } //assume expensive to look up
}
显然我有办法做到这一点,两个不同的控制器,返回不同的数据视图。我的问题是,这样可以吗还是有更好的方法?
无论如何我都可以这样做:http://host/api/products/id?includeprice=true 并使用它来返回替代模型?这是个好主意吗?
【问题讨论】:
标签: rest asp.net-web-api