簡體   English   中英

如何獲取特定頁面類型構建器類型的 EPiServer 頁面,並正確填充其所有強類型屬性?

[英]How do I get an EPiServer page of a specific page type builder type with all its strongly typed properties correctly populated?

如何正確填充所有強類型屬性的特定頁面類型構建器類型的 EPiServer 頁面? 我可以在一個方法調用中做到這一點嗎?

我試過使用:

ContentPageType pageAsContentPageType =  
    DataFactory.Instance.GetPage<ContentPageType>(page.PageLink);

但是,這不起作用。 盡管我將其目標指定為 ContentPageType,但它只填充 PageData 屬性。

我在下面包含了有問題的代碼,我對此進行了廣泛的評論:

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;
}

編輯:

以前,我嘗試了以下方法,但它也不起作用:

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>()不起作用。 因此,重新獲取每一頁就像 Joel 所說的那樣。

帶有 type 參數的 GetPage 方法是在 EPiServer CMS 5 的最后一個版本中引入的,並在版本 6 中被刪除。因此,假設您使用的是版本 5 而不是自定義擴展方法,答案就是不使用帶有類型的方法參數,而只是將調用結果轉換為 GetPage(假設您知道類型)。 換句話說,下面的代碼應該可以正常工作:

ContentPageType pageAsContentPageType = (ContentPageType) DataFactory.Instance.GetPage(page.PageLink);

Page Type Builder 攔截對 GetPage 的調用並將返回的 PageData object 替換為正確類型的代理。 此代理攔截對屬性的調用並返回值。 換句話說,PTB 類的實例從未真正填充,但 PTB 可以攔截調用至關重要。

帶有 type 參數的 GetPage 方法在某種程度上是實驗性的,雖然它確實允許將頁面作為特定類型返回,但它不允許外部方(例如 PTB)替換返回的 object。 后來根據請求將其刪除,以允許我們創建具有相同簽名的擴展方法。

這里有一些歷史。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM