【发布时间】:2014-04-12 06:44:22
【问题描述】:
有没有人知道如何以编程方式检索可以分配给 Episerver 中页面的类别? C# 是我正在使用的编程语言,但 VB 中的示例也可以。
【问题讨论】:
标签: c# content-management-system categories episerver
有没有人知道如何以编程方式检索可以分配给 Episerver 中页面的类别? C# 是我正在使用的编程语言,但 VB 中的示例也可以。
【问题讨论】:
标签: c# content-management-system categories episerver
如果您是在 CMS 中定义的所有类别之后,那么首先获取根类别及其所有子类别。
Category rootCategory = Category.GetRoot();
CategoryCollection childCategories = rootCategory.Categories;
foreach (Category category in childCategories)
{
// do whatever
}
如果您只想检索当前页面上选择的类别,则遍历当前页面上的 Category 属性。它返回一个 CategoryList 对象,其中包含所选类别的 Id。
foreach (int catId in CurrentPage.Category)
{
Category category = Category.Find(catId);
// do whatever
}
【讨论】:
Category.GetRoot() 根据 Episerver 9.0 已过时
由于Category.GetRoot() 被标记为已过时,因此根据 Episerver 9,此解决方案更合适:
var categoryRepo = ServiceLocator.Current.GetInstance<CategoryRepository>();
var rootCategory = categoryRepo.GetRoot();
CategoryCollection childCategories = rootCategory.Categories;
foreach (Category category in childCategories)
{
// do whatever
}
【讨论】:
您使用 EPiServer.DataAbstraction.Category 类。 Category.GetRoot() 方法是一个不错的起点:
此页面上也有代码示例:
【讨论】: