Why my OData answer does not have navigation properties

If you look at the following oData file example, which you will see, enable the navigation properties for the "children" elements to tell you which URL should be:

http://services.odata.org/OData/OData.svc/Suppliers?$format=json

For example, provider 0 has a navigation property for products. This refers to the product list for this vendor.

http://services.odata.org/OData/OData.svc/Suppliers (0) / Products? $ format = json


I am trying to do the same with ODataConventionModelBuilder and EntitySetController<Product> so that when I request oData/Product(0) it will show me the โ€œfunctionsโ€ for the product:

I create my model as follows (based on GetImplicitEdmModel example )

  // 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); 

I am creating 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; } } 

When I actually call the URL for my child property, it works and gives me the correct list of functions:

  /oData/Products(18)/ProductFeatures 

However, I would suggest that the navigation property in /oData/Products(18) points to this.

What I need to do to make it appear. This article says that it is automatic, but I do not see them:

ODataConventionModelBuilder, which is usually recommended by ODataModelBuilder, automatically infers hierarchy inheritance in the absence of an explicit configuration. Then, as soon as the hierarchy, it will also define properties and property navigation too. This allows you to write less code, focusing on where you deviate from our conventions.

+4
source share
1 answer

I think the problem is that you are requesting application/json . application/json in the OData web interface points to json light, which is the latest JDD OData view aimed at reducing the size of the response payload and trimming unnecessary / redundant metadata from the response. For comparison, try getting url ~/oData/Products(18) with the header accept application/json;odata=verbose .

Now the idea of โ€‹โ€‹json light is that if a link can be computed because the link follows conventions, it will not be placed in the response. A perfect example of this is the navigation link /oData/Products(18)/ProductFeatures . This follows the OData uri conventions.

OData json light has 3 modes, minimal metadata (default), data in fullmetadata and nometadata formats. The names themselves explain. If you want the link to be on the wire, send a request with the header accept application/json;odata=fullmetadata .

Refer to this document to learn more about json light.

+14
source

Source: https://habr.com/ru/post/1479863/


All Articles