为什么我的oData响应没有导航属性 [英] Why doesn't my oData response have navigation properties
问题描述
如果您查看以下示例oData feed,则会看到子项"包含的导航属性,以告诉您要遵循的URL:
If you look at the following sample oData feed you'll see included navigation properties for 'child' items to tell you which URL to follow:
http://services.odata.org/OData/OData .svc/Suppliers?$ format = json
例如,供应商0具有产品的导航属性. 这链接到该供应商的产品列表.
For example supplier 0 has a navigation property to products. This links to a list of products for that supplier.
http://services.odata .org/OData/OData.svc/Suppliers(0)/Products?$ format = json
我正在尝试对ODataConventionModelBuilder
和EntitySetController<Product>
做同样的事情,因此当我请求oData/Product(0)
时,它将向我显示产品的功能":
I'm trying to do the same with ODataConventionModelBuilder
and EntitySetController<Product>
so that when I request oData/Product(0)
it will show me the 'features' for the product:
我这样创建模型(基于 GetImplicitEdmModel样本)
I create my model like this (based on 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创建一个控制器:
I create a controller for 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;
}
}
当我实际调用child属性的URL时,它可以工作并为我提供正确的功能列表:
When I actually call the URL for my child property it works and gives me the correct list of features :
/oData/Products(18)/ProductFeatures
但是我希望在/oData/Products(18)
中有一个导航属性指向这一点.
However I would have expected a navigation property in /oData/Products(18)
pointing to this.
What do I need to do to get this to appear. This article says it's automatic but I'm not seeing them:
ODataConventionModelBuilder,通常建议在 ODataModelBuilder,将自动推断继承 没有显式配置的层次结构.然后一旦 层次结构被推断,它也将推断属性和导航 属性.这使您可以编写更少的代码,重点放在哪里 您偏离了我们的惯例.
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.
推荐答案
我认为问题是您要使用application/json
. Web API OData中的application/json
指向json light,这是最新的OData json表示形式,旨在减小响应有效负载大小并修剪响应中不必要/多余的元数据.为了进行比较,请尝试获取带有接受标头application/json;odata=verbose
的网址~/oData/Products(18)
.
I think the problem is that you are asking for application/json
. application/json
in web API OData points to json light which is the latest OData json representation aimed at reducing response payload sizes and trimming the un-necessary/redundant metadata from the response. For comparison, try getting the url ~/oData/Products(18)
with accept header application/json;odata=verbose
.
现在,json light背后的思想是,如果可以根据链接来计算链接,那么它就不会被放入响应中.导航链接/oData/Products(18)/ProductFeatures
是一个很好的例子.它遵循OData uri约定.
Now, the idea behind json light is that if a link can be calculated because the link follows conventions, it will not be put in the response. The navigation link /oData/Products(18)/ProductFeatures
is a perfect example of that. It follows OData uri conventions.
OData json灯具有3种模式,minimummetadata(默认),fullmetadata和nometadata.名称本身是说明性的.如果您希望链接处于在线状态,请发送带有接受标头application/json;odata=fullmetadata
的请求.
OData json light has 3 modes, minimalmetadata (the default), fullmetadata and nometadata. The names are themselves explanatory. If you want the link to be on the wire, send the request with accept header application/json;odata=fullmetadata
.
请参阅此文档以了解有关json light的更多信息.
Refer to this document to understand more about json light.
这篇关于为什么我的oData响应没有导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!