我正在致力于实现内容交付 API。响应的问题之一是与前端应用程序不相关的不需要的字段列表。这是 Optimizely 的一篇文章,用于自定义数据。
这里的问题是,
CustomContentResultService
类没有被调用。
我正在开发的 Episerver 版本是 11
假设您正在使用 CMS 11,您想查看
ContentModelMapperBase
。
但是,内容交付 api 输出的字段的序列化也会遵循
[JsonIgnore]
等常见属性,例如
[JsonIgnore]
[Display(Name="Ignore Me")]
public virtual XHtmlString IgnoredContent { get; set; }
如果您想完全控制您的 Content Delivery API,请创建一个继承
ExtendedContentModelMapper
并实现 ContentModelMapperBase
方法的 TransformContent
。
public class ExtendedContentModelMapper : ContentModelMapperBase
{
public ExtendedContentModelMapper(
IContentTypeRepository contentTypeRepository,
ReflectionService reflectionService,
IContentModelReferenceConverter contentModelService,
IUrlResolver urlResolver,
IEnumerable<IPropertyModelConverter> propertyModelConverters,
IContentVersionRepository contentVersionRepository,
ContentLoaderService contentLoaderService)
: base(
contentTypeRepository,
reflectionService,
contentModelService,
urlResolver,
propertyModelConverters,
contentVersionRepository,
contentLoaderService)
{
// other injections
}
public override ContentApiModel TransformContent(IContent content, bool excludePersonalizedContent, string expand)
{
var contentModel = base.TransformContent(content, excludePersonalizedContent, expand);
// magic stuff
contentModel.Properties.Remove("IgnoredContent");
contentModel.Properties.Remove("Title");
contentModel.Properties.Remove("WhateverProperites");
contentModel.Properties.Add("FakeProperty", "This property is created virtually");
if(content is ArticlePage)
{
// code
}
return contentModel;
}
}
不要忘记注入 contentModelMapper 例如。
context.Services.AddTransient<IContentModelMapper, ExtendedContentModelMapper>();