4

厳密に型指定されたすべてのプロパティが正しく取り込まれた、特定のページ タイプ ビルダー タイプの EPiServer ページを取得するにはどうすればよいですか? 1 回のメソッド呼び出しでこれを行うことはできますか?

私は使用してみました:

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>()動作しません。したがって、ジョエルが言ったように、各ページを再取得することは機能します。

4

1 に答える 1

4

type パラメーターを持つ GetPage メソッドは、EPiServer CMS 5 の最後のバージョンで導入され、バージョン 6 で削除されました。したがって、カスタム拡張メソッドではなくバージョン 5 を使用していると仮定すると、答えは単純に型を持つメソッドを使用しないことです。代わりに、GetPage への呼び出しの結果をキャストするだけです (型がわかっていると仮定します)。つまり、以下のコードは正常に動作するはずです。

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

Page Type Builder は GetPage の呼び出しをインターセプトし、返された PageData オブジェクトを正しいタイプのプロキシに置き換えます。このプロキシは、プロパティへの呼び出しをインターセプトし、値を返します。つまり、PTB クラスのインスタンスが実際に読み込まれることはありませんが、PTB が呼び出しをインターセプトできることが重要です。

type パラメーターを指定した GetPage メソッドは多少実験的なもので、ページを特定の型として返すことはできましたが、返されるオブジェクトを外部の関係者 (PTB など) で置き換えることはできませんでした。その後、リクエストごとに削除され、同じ署名を持つ拡張メソッドを作成できるようになりました。

ここにいくつかの歴史があります。

于 2011-07-09T17:43:43.873 に答える