Search code examples
asp.net-web-apiodata

Microsoft ODataController response with @odata.context


I'm using these packages:

<package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.7.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />

I inserted an ODataController using this technique:

  1. Right-click on Controller folder, "Add Controller"
  2. Choose from "Add Scaffold" list the "Web API 2 ODataController with actions, using Entity Framework"
  3. Choose the appropriate Model and context for this ODataController
  4. Modify WebApiConfig.cs according to the inserted template

When I hit my controller, the result does not include the @odata.context property, which throws off the angular-odataresources library I'm using

The response only includes these two properties: {odata.metadata: "...", value: [...]}

How can I make the ODataController respond with @odata.context property?

I tried using this, but no change in response: $format=application/json;odata.metadata=full

Update

I just found this link Specifically:

The name of the odata.context annotation is "odata.metadata" and is written without the leading "@"

Does this mean I cannot make ODataController return @odata.context?

As a workaround I can always change the Javascript which is thrown off.


Solution

  • Microsoft.AspNet.WebApi.OData supports OData versions 1-3, but the @odata.context annotation is specific to version 4. Also, the JSON payload formats are significantly different between versions 3 and 4.

    Since you own the service code, your best best is to upgrade the OData implementation to v4. It looks like that's what ODataAngularResources is expecting anyway. Uninstall Microsoft.AspNet.WebApi.Client and install Microsoft.AspNet.OData.