Как получить страницу EPiServer с определенным типом компоновщика страниц со всеми его строго типизированными свойствами, правильно заполненными? Могу ли я сделать это за один вызов метода?
Я пытался использовать:
ContentPageType pageAsContentPageType =
DataFactory.Instance.GetPage<ContentPageType>(page.PageLink);
Однако это не работает. Он заполняет свойства PageData, но не более того, несмотря на то, что я указываю его цель как ContentPageType.
Я включил код обидчика ниже, который я подробно прокомментировал:
public static IList<ContentPageType> GetMapEnabledPages()
{
// Get the content pages
IList<PageData> pages = PageFactory.GetPages(
PageReference.StartPage.ID,
BaseSettings.Constants.EPiServer.PageTypeNames.ContentPage
);
// Some content pages will be map enabled pages. So, we need to extract the ones that are put them in this variable.
IList<ContentPageType> mapEnabledPages = new List<ContentPageType>();
// walk pages to extract only map enabled pages
foreach (PageData page in pages)
{
// get the page as a ContentPageType.
// unfortunately, this method only populates the PageData information and none of the additional strongly types properties that a ContentPageType has.
// we would expect this happen because EPiServer uses IoC elsewhere but does not do it here.
ContentPageType pageAsContentPageType =
DataFactory.Instance.GetPage<ContentPageType>(page.PageLink);
// So, we fudge it - we know that the PageData weakly type properties has IsMapEnabled correctly populated.
// So, we put that value in the strongly typed ContentPageType property.
if (pageAsContentPageType != null && pageAsContentPageType["IsMapEnabled"] != null)
{
// put that the weakly typed property for "IsMapEnabled" into the strongly typed ContentPageType IsMapEnabled property
pageAsContentPageType.IsMapEnabled =
TypeHelper.ConvertToBoolean(pageAsContentPageType["IsMapEnabled"].ToString());
// check if it is map enabled
if (pageAsContentPageType.IsMapEnabled)
{
// it is a map enabled page. So, add it to the mapEnabledPages list.
mapEnabledPages.Add(pageAsContentPageType);
}
}
}
return mapEnabledPages;
}
EDIT:
Ранее я пробовал следующее, но оно тоже не работает:
ContentPageType pageAsContentPageType =
DataFactory.Instance.GetPage(page.PageLink) as ContentPageType
РЕШЕНИЕ ДЛЯ CMS5R2SP2:
Оказалось, что в свойстве типа страницы IsMapEnabled отсутствует виртуальное ключевое слово. Поэтому контейнер IoC не переопределял это свойство из значения по умолчанию. Вот окончательная реализация:
IList<PageData> pages = PageFactory.GetPages(
PageReference.StartPage.ID,
BaseSettings.Constants.EPiServer.PageTypeNames.ContentPage
);
// Some content pages will be map enabled pages.
// So, we need to extract the ones that are put them in this variable.
IEnumerable<ContentPageType> mapEnabledPages =
from page in pages.OfType<ContentPageType>()
where page.IsMapEnabled
select page;
// return map enabled pages.
return mapEnabledPages.ToList();
РЕШЕНИЕ ДЛЯ CMS6:
OfType<ContentPageType>()
не работает. Итак, перезапись каждой страницы работает, как сказал Джоэл.