加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:14:19 所属栏目:asp.Net 来源:网络整理
导读:如果您查看以下示例oData Feed,您会看到包含“子”项的导航属性,以告诉您要遵循的URL: 07000 例如,供应商0具有产品的导航属性. 这链接到该供应商的产品列表. 07001 我试图用ODataConventionModelBuilder和EntitySetController Product做同样的事情.所以当我
如果您查看以下示例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灯的更多信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读