Locate() 的 EPiServer 7 命名空间未解析



我是EPiServer的新手,正在尝试从我创建的新闻列表中检索子新闻文章页面。 我在网上找到的示例使用了 Locate() 方法,但是当我尝试将 Locate 应用于我的代码时,找不到它。

这是我看过的文章之一。http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/8/EPiServer7-Working-with-IContentRepositoryDataFactory/

从本质上讲,我只需要为新闻项目列表返回子文章列表,因此我尝试的方法可能一开始就不正确。

无论如何,这是我当前使用 using 语句的模型。

using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.ServiceLocation;
using EPiServer.SpecializedProperties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace EPiServerExercise.Models.Pages
{
    [ContentType(DisplayName = "News List", GUID = "ac3287b1-4d78-4eb3-bad2-6b5c43530b33", Description = "")]
    public class NewsList : BasePage
    {
        private IEnumerable<NewsArticle> getNewsArticles(NewsList currentPage)
        {

            //var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>

            //IEnumerable<NewsArticle> newsArticles = new List<NewsArticle>();
            //PageReference pageLink = currentPage.ParentLink;
            //IEnumerable<NewsArticle> newsArticles = Locate.ContentRepository().GetChildren<IContent>(pageLink);
            //IEnumerable<NewsArticle> newsArticles = ServiceLocationHelperExtensions.
            //var serviceLocationHelper = ServiceLocator.Current.GetInstance();
            //serviceLocationHelper.ContentLoader
        }
    }
}

我缺少什么参考来解析 Locate() 方法? 我们正在使用 EPiServer 7 和 MVC。 感谢您的帮助。

更新 11/18/2014

这是我放入模型中的最终解决方案。 这与Vsevolod Goloviznin的建议几乎相同。谢谢。

public string showNewsArticles()
{
    IEnumerable<NewsArticle> newsArticles = getNewsArticles(this);
    // Code to loop through the articles
}
private IEnumerable<NewsArticle> getNewsArticles(NewsList currentPage)
{
    var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
    IEnumerable<NewsArticle> newsArticles = repository.GetChildren<NewsArticle>(currentPage.ContentLink);
    return newsArticles;
}

看起来他只是用工厂来IContentRepository,忘了提。因此,要获得相同的功能,您可以使用ServiceLocator获取IContentRepository,然后获取页面的所有子项:

var service = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
var pages = service.GetChildren<NewsArticle>(currentPage.ParentLink).ToList();

最新更新