Prefer odata.include-annotations

Applies To:yes OData AspNet WebApi V7yes OData AspNet WebApi V6

Since OData WebApi V5.6, it supports odata.include-annotations.

odata.include-annotations

It supports the following four templates:

  1. odata.include-annotations="*" // all annotations
  2. odata.include-annotations="-*" // no annotations
  3. odata.include-annotations="display.*" // only annotations under "display" namespace
  4. odata.include-annotations="display.subject" // only annotation with term name "display.subject"

Let's have examples:

odata.include-annotations=*

We can use the following codes to request all annotations:

HttpRequestMessage request = new HttpRequestMessage(...);
request.Headers.Add("Prefer", "odata.include-annotations=*");
HttpResponseMessage response = client.SendAsync(request).Result;
...

The response will have all annotations:

{  
  "@odata.context":"https://localhost:8081/$metadata#People/$entity",
  "@odata.id":"https://localhost:8081/People(2)",
  "Entry.GuidAnnotation@odata.type":"#Guid",
  "@Entry.GuidAnnotation":"a6e07eac-ad49-4bf7-a06e-203ff4d4b0d8",
  "@Hello.World":"Hello World.",
  "PerId":2,
  "Property.BirthdayAnnotation@odata.type":"#DateTimeOffset",
  "Age@Property.BirthdayAnnotation":"2010-01-02T00:00:00+08:00",
  "Age":10,
  "MyGuid":"f99080c0-2f9e-472e-8c72-1a8ecd9f902d",
  "Name":"Asha",
  "FavoriteColor":"Red, Green",
  "Order":{  
    "OrderAmount":235342,"OrderName":"FirstOrder"  
  }  
}

odata.include-annotations=Entry.*

We can use the following codes to request specify annotations:

HttpRequestMessage request = new HttpRequestMessage(...);
request.Headers.Add("Prefer", "odata.include-annotations=Entry.*");
HttpResponseMessage response = client.SendAsync(request).Result;
...

The response will only have annotations in "Entry" namespace:

{  
  "@odata.context":"https://localhost:8081/$metadata#People/$entity",
  "@odata.id":"https://localhost:8081/People(2)",
  "Entry.GuidAnnotation@odata.type":"#Guid",
  "@Entry.GuidAnnotation":"a6e07eac-ad49-4bf7-a06e-203ff4d4b0d8",
  "PerId":2,
  "Age":10,
  "MyGuid":"f99080c0-2f9e-472e-8c72-1a8ecd9f902d",
  "Name":"Asha",
  "FavoriteColor":"Red, Green",
  "Order":{  
    "OrderAmount":235342,"OrderName":"FirstOrder"  
  }  
}