Customize Control Information for full metadata requests in odata.net

kamanuja

Kanish

Background

OData supports three metadata levels for the JSON format, namely –

  1. No Metadata
  2. Minimal Metadata
  3. Full Metadata

No Metadata as the name suggests does not include control information other than nextLink and count. Minimal metadata responses usually include context, etag, deltalink etc. whereas full metadata responses contain all the control information like navigationLink, assciationLink, readLink, mediaReadLink etc.

ODataMetadataSelector

With odata.net >=7.6.1, it will be possible to customize what metadata gets written out. A new class, ODataMetadataSelector is introduced which exposes methods that can be overridden to add or remove control information related to the navigation properties, stream properties, and operations.

 

The instance of this class can be set on ODataMessageWriterSettings’s property, MetadataSelector. Since ODataMessageWriterSettings can be dependency injected, you can also use dependency injection for ODataMetadataSelector by injecting a custom ODataMessageWriterSettings.

Since this will get called for every resource being requested, it is highly recommended to cache the results if possible.

Usage Example

You can choose to annotate properties of types for which you don’t want to write out the metadata. This sample here will omit control information for all such navigation properties. You can also have arbitrary criteria to select the metadata such as omitting all actions.

 

kamanuja
Kanish Manuja

Follow Kanish   

No Comments.