如何检查页面或节点 (IPublishedContent) 是否使用特定的继承文档类型?

How do I check if a page or node (IPublishedContent) uses a specific inherited document type?

我有一个文档类型 Page,它继承自父文档类型 Page Base

在呈现的页面上,我可以查看文档类型是否等于 Page 文档类型别名:

// Where Model is type Umbraco.Web.Models.RenderModel.
if (Model.Content.DocumentTypeAlias == "page")
{
    // My current page uses the "page" document type.
}

但是我如何检查我的 IPublishedContent 是否继承自 Page Base

原来在 IPublishedContent 类型上有一个方法:

// Where Model is type Umbraco.Web.Models.RenderModel.
if (Model.Content.IsDocumentType("pageBase", true))
{
    // My current page uses the "pageBase" document type.
}

.IsDocumentType() 上接受布尔值的重载称为 recursive,它会检查您的 IPublishedContent 是提供的文档类型还是派生自该文档类型.

更新:但是!!

如果您的文档类型存储在文件夹中,则使用递归 .IsDocumentType() 会出现错误。此问题已记录 here

相反,您可以使用 .IsComposedOf():

// Where Model is type Umbraco.Web.Models.RenderModel.
if (Model.Content.IsComposedOf("pageBase"))
{
    // My current page uses the "pageBase" document type.
}

此方法同时检查文档类型组成和继承。

此方法不同于递归 .IsDocumentType(),因为它仅检查 inheritance/compositions,而不检查当前文档类型是否为提供的别名。