I am working on implementing content delivery API. One of the issue with the response is the list of unwanted fields that are not relevant for the front-end application. Here's an article from Optimizely to customize the data.
The issue here is, CustomContentResultService class is not getting called.
The Episerver Version that I am working on is 11
[InitializableModule]
public class DependencyResolverInitialization : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
//Implementations for custom interfaces can be registered here.
context.ConfigurationComplete += (o, e) =>
{
//Register custom implementations that should be used in favour of the default implementations
context.Services.AddTransient<IContentRenderer, ErrorHandlingContentRenderer>()
.AddTransient<ContentAreaRenderer, XYZContentAreaRenderer>()
.AddTransient<ContentResultService, CustomContentResultService>();
};
context.Services.Configure<ContentApiConfiguration>(config =>
{
config.Default()
.SetMinimumRoles(string.Empty)
.SetMultiSiteFilteringEnabled(false)
.SetSiteDefinitionApiEnabled(true)
.SetIncludeMasterLanguage(false)
.SetFlattenPropertyModel(true)
.SetIncludeNullValues(false)
.SetValidateTemplateForContentUrl(false);
});
}
public void Initialize(InitializationEngine context)
{
DependencyResolver.SetResolver(new ServiceLocatorDependencyResolver(context.Locate.Advanced));
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}
CustomContentResultService class is exactly how it is implemented in
Assuming you are using CMS 11 you want to look at
ContentModelMapperBase.However, the serialization of fields outputted by the content delivery api will also follow common attributes like
[JsonIgnore], e.g.If you want full control of your Content Delivery API create an
ExtendedContentModelMapperthat inheritContentModelMapperBaseand implement theTransformContentmethod.Don't forget to inject the contentModelMapper E.g.
context.Services.AddTransient<IContentModelMapper, ExtendedContentModelMapper>();