OData: Change URL value for entity type in EDM Model

1.1k Views Asked by At

I'm working with a ASP.NET Core Web Api project that uses OData for the exposed endpoints and are consumed with a Simple.OData.Client.

Some of my endpoints are:

http://{baseUrl}/odata/Vehicle --> this works perfectly

But I'm having issues with these two:

http://{baseUrl}/odata/Vehicle/Brand

http://{baseUrl}/odata/Vehicle/Type

Basicly, I can't modify my EDM Models for modifying the URL property that is exposed in the metadata of OData. My EDM looks like this:

private IEdmModel GetEdmModel()
{
    var odataBuilder = new ODataConventionModelBuilder();   

    odataBuilder.EntitySet<Vehicle>("Vehicle");
    odataBuilder.EntitySet<VehicleType>("VehicleType");
    odataBuilder.EntitySet<VehicleBrand>("VehicleBrand");

    return odataBuilder.GetEdmModel();
}

And the metadata that I get when I navigate through http://{baseUrl}/odata/ is the following:

{
    "@odata.context": "https://localhost:44332/odata/$metadata",
    "value": [
        {
            "name": "Vehicle",
            "kind": "EntitySet",
            "url": "Vehicle"
        },
        {
            "name": "VehicleType",
            "kind": "EntitySet",
            "url": "VehicleType"
        },
        {
            "name": "VehicleBrand",
            "kind": "EntitySet",
            "url": "VehicleBrand"
        }
    ]
}

I couldn't find a way to maintain the name as it is, but modify the "url" property shown on the JSON to point to my proper endpoint. I want this result:

    {
        "name": "VehicleBrand",
        "kind": "EntitySet",
        "url": "Vehicle/Brand"
    }

Any of the methods exposed on EntitySetConfiguration or ODataConventionModelBuilder seems to have a way to specify a different URI for a registered entity type.

Someone has faced this issue? I'm sure that might be some way of solving this.

1

There are 1 best solutions below

2
Michael Schönbauer On BEST ANSWER

Odata Route or Navigation Property?

Please have a look at that documentation here

Long story short - an OData URI consists of:

  • The service root
  • The OData path
  • Query options

For example. This is a Path that goes to the EntitySet "Products", takes the first, and then Navigates (see Navigation Properties) to its Supplier.

https://example.com/odata/Products(1)/Supplier?$top=2
------------base---------|-----Path-----------?---options---

So, everything you make accessible at root level should have its own path, and the / telling Odata to navigate onward from there.

So, now for OData, it would freak the hell out of most clients and surely be bad style if you would define an entitysets path as something that can be confused with another entititysets navigation property.

But if you REALLY need to do it, maybe you can achieve it by defining a custom routing convention.

But dont! It will only make trouble

Do you want a navigation property?

If you want the set that "Type" returns to be dependent on the Vehicle, you should define a navigation property on Vehicle instead.

Greetings, Mike