Skip to content

How to add a dynamic property to my OData response which lives outside the Entity Type and is not included in the EDM Model? #2854

Open
@sihanook

Description

@sihanook

I have a generically typed OData controller that is able to handle requests from multiple routes/entities. This is hooked up using custom IApplicationFeatureProvider<ControllerFeature> and IControllerModelConvention implementations, similar to the way it is described in the blog (in Approach 2).

My EDM model has 2 entities: one and two. They have different properties. I have this setup to access these entity sets by using routes like:

  • /api/one
  • /api/two
  • /api/one?$filter=id eq 2&$select=id,name&$count=true

The current API response looks some like below:

{
  "@odata.context": "http://localhost/api/$metadata#one(Id,Name)",
  "@odata.count": 1,
  "value": [
    {
      "Id": "2",
      "Name": "My name 2"
    }
  ]
}

The code in this above state is available at the location here: https://github.com/sihanook/dynamicodataroutingsample

This all works fine except that now I want to add an extra property with some additional metadata about the result set to the response. So, I want the response from the API to look like below, but still be able to use the routes as above:

{
  "@odata.context": "http://localhost/api/$metadata#one(Id,Name)",
  "@odata.count": 1,
  "value": {
    "content": [
      {
        "Id": "2",
        "Name": "My name 2"
      }
    ],
    "additionalmetadata": {
      "someProperty1": "someValue 1",
      "someProperty2": "someValue 2"
    }
  }
}

What is the correct way to achieve this in .NET Core OData? I am using version 9.0.0.

Assemblies affected

I am using "Microsoft.AspNetCore.OData" version "9.0.0".

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions