微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wcf-data-services – 为什么我的oData响应没有导航属性

如果您查看以下示例oData Feed,您会看到包含“子”项的导航属性,以告诉您要遵循的URL:

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.

解决方法

我认为问题是你要求application / json. web API中的application / json OData指向json light,这是最新的OData json表示,旨在减少响应有效负载大小并从响应中修剪不必要/冗余元数据.为了比较,尝试使用accept header application / json获取url~ / oData / Products(18); odata = verbose.

现在,json light背后的想法是,如果链接可以计算,因为链接遵循约定,它将不会被放入响应中.导航链接/ oData / Products(18)/ ProductFeatures就是一个很好的例子.它遵循OData uri惯例.

OData json light有3种模式,minimalMetadata(认),fullMetadata和noMetadata.名称本身就是解释性的.如果您希望链接在线路上,请使用accept header application / json; odata = fullMetadata发送请求.

请参阅此document以了解有关json灯的更多信息.

原文地址:https://www.jb51.cc/aspnet/251767.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐