【问题标题】:Programmatically retrieve the categories that can be assigned to pages in Episerver以编程方式检索可以分配给 Episerver 中页面的类别
【发布时间】:2014-04-12 06:44:22
【问题描述】:

有没有人知道如何以编程方式检索可以分配给 Episerver 中页面的类别? C# 是我正在使用的编程语言,但 VB 中的示例也可以。

【问题讨论】:

    标签: c# content-management-system categories episerver


    【解决方案1】:

    如果您是在 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 已过时
    【解决方案2】:

    由于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
    }
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 2018-08-30
        • 2010-11-04
        • 2014-06-06
        • 2012-04-10
        相关资源
        最近更新 更多