07000
例如,供应商0具有产品的导航属性.
这链接到该供应商的产品列表.
07001
我试图用ODataConventionModelBuilder和EntitySetController< Product>做同样的事情.所以当我请求oData / Product(0)时,它会显示产品的’features’:
我这样创建我的模型(基于GetImplicitEdmModel sample)
// odata ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<RRStoreDB.Models.Product>("Product"); modelBuilder.EntitySet<RRStoreDB.Models.ProductFeature>("ProductFeature"); Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel(); config.Routes.MapODataRoute("ODataRoute","odata",model);
我为WebAPI创建了一个控制器:
public class ProductController : EntitySetController<Product,int> { RRStoreDBContext _db = new RRStoreDBContext(); [Queryable] public override IQueryable<DProduct> Get() { return _db.Products.AsQueryable(); } public ICollection<ProductFeature> GetProductFeatures(int key) { Product product = _db.Products.FirstOrDefault(p => p.ProductId == key); if (product == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return product.ProductFeatures; } }
当我实际调用我的子属性的URL时,它工作并给我正确的功能列表:
/oData/Products(18)/ProductFeatures
但是我希望/ oData / Products(18)中的导航属性指向这个.
我需要做些什么才能让它出现. This article说它是自动的,但我没有看到它们:
The ODataConventionModelBuilder,which is generally recommended over
the ODataModelBuilder,will automatically infer inheritance
hierarchies in the absence of explicit configuration. Then once the
hierarchy is inferred,it will also infer properties and navigation
properties too. This allows you to write less code,focusing on where
you deviate from our conventions.
解决方法
现在,json light背后的想法是,如果链接可以计算,因为链接遵循约定,它将不会被放入响应中.导航链接/ oData / Products(18)/ ProductFeatures就是一个很好的例子.它遵循OData uri惯例.
OData json light有3种模式,minimalMetadata(默认),fullMetadata和noMetadata.名称本身就是解释性的.如果您希望链接在线路上,请使用accept header application / json; odata = fullMetadata发送请求.
请参阅此document以了解有关json灯的更多信息.