【发布时间】:2021-06-03 05:04:18
【问题描述】:
episerver 中有没有办法找到块层次结构。 我的结构是 Carousal Block 包括 Carousal Items 的 ContentArea 属性 狂欢项目1 细绳 选厂 狂欢项目2 细绳 选厂 在 Carousal Item 1 中,我有一个选择工厂。我需要在这个选择工厂中获取 Carousal Block 属性。 任何帮助表示赞赏。 提前致谢。
【问题讨论】:
标签: episerver
episerver 中有没有办法找到块层次结构。 我的结构是 Carousal Block 包括 Carousal Items 的 ContentArea 属性 狂欢项目1 细绳 选厂 狂欢项目2 细绳 选厂 在 Carousal Item 1 中,我有一个选择工厂。我需要在这个选择工厂中获取 Carousal Block 属性。 任何帮助表示赞赏。 提前致谢。
【问题讨论】:
标签: episerver
简短的回答,不,没有办法,因为块不会以这种方式继承结构。它们可以在任何地方使用。因此,如果您请求块的父级,它将始终返回 SysContentAssetFolder。
为了解决您的问题(我敢肯定还有其他方法),我会选择一种使用方法。例如。块在哪里使用并找到所有者,块实例将父母称为所有者,因为它是一个可以有多个父母/所有者的实例。
现在我们知道这是一些代码
public class ImageBlockController : BlockController<ImageBlock>
{
private readonly IContentRepository contentRepository;
private readonly IContentTypeRepository contentTypeRepository;
public ImageBlockController(IContentRepository contentRepository, IContentTypeRepository contentTypeRepository)
{
this.contentRepository = contentRepository;
this.contentTypeRepository = contentTypeRepository;
}
public override ActionResult Index(ImageBlock currentBlock)
{
var blockContent = (IContent)currentBlock;
// Get block references
var usageReferences = contentRepository.GetReferencesToContent(blockContent.ContentLink, false);
if(usageReferences.Count() == 1)
{
// easy peasy, only one usage
var owner = contentRepository.Get<IContent>(usageReferences.First().OwnerID);
// example
var ownerType = contentTypeRepository.Load(owner.ContentTypeID);
if (ownerType.ModelType == typeof(ImageBlock))
{
// do stuff
}
}
else
{
// handle if the block is used at multiple locations
// ...
}
return PartialView(currentBlock);
}
}
本例中的技巧是获取所有对已加载块的引用contentRepository.GetReferencesToContent,如果我们对结果感到满意,请执行某些操作并继续。如果您有多个usageReferences,您可能必须在加载的页面中搜索正确的所有者块。在这种情况下,加载IPageRouterHelper,使用var parentPage = contentRepository.Get<IContent>(pageRouteHelper.Page.ContentLink); 之类的内容获取当前页面,并将usageReferences 与内容区域中的项目匹配。
【讨论】: