检查块是否使用个性化
Check if block uses personalization
是否可以通过编程方式确定块的内容是否使用个性化?
我需要知道何时可以针对不同的访问者组对块的内容进行个性化设置,以便正确处理缓存。
为了做出有关个性化的决定,您需要访问 ContentAreaItem
实例本身。
访问 ContentAreaItem
的最简单方法是挂接到渲染管道。您可能需要将 ContentAreaRenderer
(请参阅代码片段的 AlloyTech 示例站点)替换为您自己的实现。
然后你可以执行BeforeRenderContentAreaItemStartTag
:
public class MyCustomContentAreaRenderer : ContentAreaRenderer
{
protected override void BeforeRenderContentAreaItemStartTag(TagBuilder tagBuilder, ContentAreaItem contentAreaItem)
{
var isPersonalizationApplied = contentAreaItem.AllowedRoles.Any();
}
}
下一个问题当然是如何将此决定传递给块类型实例,因为覆盖的 ContentAreaRenderer
方法距离内容区域内的块实例足够远。
@keithl8041,您可以使用VisitorGroupHelper
检查当前用户是否匹配特定角色:
using EPiServer.Personalization.VisitorGroups;
using EPiServer.Security;
protected override void BeforeRenderContentAreaItemStartTag(TagBuilder tagBuilder, ContentAreaItem contentAreaItem)
{
var groupHelper = new VisitorGroupHelper();
var isPersonalizationApplied = contentAreaItem.AllowedRoles.Any();
var isCurrentUserInAnyOfGroups = contentAreaItem.AllowedRoles.Any(r => groupHelper.IsPrincipalInGroup(PrincipalInfo.CurrentPrincipal, r);
}
是否可以通过编程方式确定块的内容是否使用个性化?
我需要知道何时可以针对不同的访问者组对块的内容进行个性化设置,以便正确处理缓存。
为了做出有关个性化的决定,您需要访问 ContentAreaItem
实例本身。
访问 ContentAreaItem
的最简单方法是挂接到渲染管道。您可能需要将 ContentAreaRenderer
(请参阅代码片段的 AlloyTech 示例站点)替换为您自己的实现。
然后你可以执行BeforeRenderContentAreaItemStartTag
:
public class MyCustomContentAreaRenderer : ContentAreaRenderer
{
protected override void BeforeRenderContentAreaItemStartTag(TagBuilder tagBuilder, ContentAreaItem contentAreaItem)
{
var isPersonalizationApplied = contentAreaItem.AllowedRoles.Any();
}
}
下一个问题当然是如何将此决定传递给块类型实例,因为覆盖的 ContentAreaRenderer
方法距离内容区域内的块实例足够远。
@keithl8041,您可以使用VisitorGroupHelper
检查当前用户是否匹配特定角色:
using EPiServer.Personalization.VisitorGroups;
using EPiServer.Security;
protected override void BeforeRenderContentAreaItemStartTag(TagBuilder tagBuilder, ContentAreaItem contentAreaItem)
{
var groupHelper = new VisitorGroupHelper();
var isPersonalizationApplied = contentAreaItem.AllowedRoles.Any();
var isCurrentUserInAnyOfGroups = contentAreaItem.AllowedRoles.Any(r => groupHelper.IsPrincipalInGroup(PrincipalInfo.CurrentPrincipal, r);
}