In this tutorial, you will learn how to filter out content-type properties that you do not want to be exposed via the content delivery API. When building any website performance matters. Having an end-point that exposes more data that is never used in the app means that the API will request will take longer than it needs to. If you could reduce the payload of every request by 20% then you would notice a big performance gain.

Whether it be a page or a block, when defining content-types within Episerver attributes are used extensively to define meta-data and validations. Out-of-the-box Episerver supports the JsonIgnoreAttribute that comes with JSON.NET. If you decorate any property on a page or block then it should be filtered out of the content delivery API.

Imagine you were building a new feature and you wanted to hide it behind a feature flag. Within a settings page within the CMS you wanted to decide what was exposed by the API or not. Using this method you could easily provide a dynamic filter on the data that the API exposes. To make it a little more obvious in code that you are doing something different define a custom attribute:

When working with the content delivery API you will likely need some custom logic that tells the pipeline how to serialize the page content into JSON. I recommend extending theContentModelMapperBase in order to do this. This is not the only way to define some custom serialization rules. As shown in the music festival sample site, you could create a class that implements the IContentModelMapper instead.

If you want to limit the properties returned by the content deliver API you can make use of ShouldPropertyBeIgnored within ContentModelMapperBase:

To extend the method to use a feature flag. You can use the content repository to check if the feature flag setting has been enabled or not.

The data should now be filtered as expected. The code above is very similar to the default behavior and I would only advise going down this route if you want to provide some more advanced property filtering. If you want to check how the default behavior works you can use DotPeek to look within EPiServer.ContentApi.Core.Serialization at the ShouldPropertyBeIgnored. Enjoy!