November Happy Hour will be moved to Thursday December 5th.

Customize API to change data returned to clients(Absolute URL & eliminating unwanted properties)

Vote:
 

Hi,
I have incorporated Content Delivery Api in my solution.The initial setup is working as expected.However there are a few changes that I am working on for tweaking the json output.
1- The Json output that i get includes a lot of properties that is irrelevant for the front end application.For instance startPublish,stopPublish,saved,status & a few others.To get an optimized output,the documentation provides an implemenation for ContentResultService which i have implemented the way as mentioned in the documentation
https://world.episerver.com/documentation/developer-guides/content-delivery-api/getting-started/how-to-customize-data-returned-to-clients/

The issue here is the ContentResultService is not triggered.
I found a thread for the same issue & have tried the recommendations given in the thread but still the same.Also, there is no further update on the thread on the resolution of the issue.Here's the link for the same.
https://world.episerver.com/forum/developer-forum/episerver-content-delivery-api/thread-container/2019/1/contentresultservice-is-executed-instead-of-the-injected-one/

2-The Json output that i get includes url which is a relative path.I need to customize the output to convert relative to absolute urls.
The solution given on the epi documentation is to customize DefaultContentModelMapper & replace with our own implementation.When trying to implement the same,i am unable to get DefaultContentModelMapper class.
The note given in the documentation says,DefaultContentModelMapper is currently marked as internal, so there is a risk of unadvertised breaking changes when customizing this
https://world.episerver.com/documentation/developer-guides/content-delivery-api/getting-started/personalizing-content/

Kindly advise.
Regards,

#254078
Apr 29, 2021 9:51
Vote:
 

Farhin,

To ignore base class properties you can ContentApiModelFilter<ContentApiModel> and set the values to null. For this to work you need to set the following property (.SetIncludeNullValues(false)) wherever you are configuring the Content API settings

Below is the example 

    [ServiceConfiguration(typeof(IContentApiModelFilter), Lifecycle = ServiceInstanceScope.Singleton)]
    public class CustomContentApiModelFilter : ContentApiModelFilter<ContentApiModel>
    {
        public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
        {
            contentApiModel.ExistingLanguages = null;
            contentApiModel.Created = null;
            contentApiModel.StartPublish = null;

        }

     }

2. To modify or transform the data being sent back to the client you need to implement DefaultContentConverter and register the custom implementation.

Register the custom implementation in your dependency

.AddSingleton<IContentModelMapper, CustomContentModelMapper>()

Below is an example to make URL's absolute.

    [ServiceConfiguration(typeof(IContentModelMapper), Lifecycle = ServiceInstanceScope.Singleton)]
    public class CustomContentModelMapper : DefaultContentConverter
    {

        public CustomContentModelMapper(IContentTypeRepository contentTypeRepository,
                                ReflectionService reflectionService,
                                IContentModelReferenceConverter contentModelService,
                                IContentVersionRepository contentVersionRepository,
                                ContentLoaderService contentLoaderService,
                                UrlResolverService urlResolverService,
                                ContentApiConfiguration apiConfig,
                                IPropertyConverterResolver propertyConverterResolver)
                            : base(contentTypeRepository,
                                  reflectionService,
                                  contentModelService,
                                  contentVersionRepository,
                                  contentLoaderService,
                                  urlResolverService,
                                  apiConfig,
                                  propertyConverterResolver)
        {
        }

        public override int Order => 200; 
        protected override string ResolveUrl(ContentReference contentLink, string language)
        {
            string resolvedUrl;
            if (_urlResolver == null)
            {
                resolvedUrl = _urlResolverService.ResolveUrl(contentLink, language);
            }
            else
            {
                resolvedUrl = _urlResolver.GetUrl(contentLink, language, new UrlResolverArguments()
                {
                    ContextMode = ContextMode.Default,
                    ForceCanonical = true
                });
            }

            if (string.IsNullOrEmpty(resolvedUrl))
            {
                return null;
            }

            if (!IsAbsoluteUrl(resolvedUrl))
            {
                resolvedUrl = UriSupport.AbsoluteUrlBySettings(resolvedUrl);
            }

            return resolvedUrl;
        }

    }

#261802
Sep 07, 2021 12:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.