これが私が思いついた解決策ですが、私の好みには少し複雑すぎます。問題は、現在表示されているページがサイトマップになく、ナビゲーション、コントロール、およびその他のロジックがサイトマッププロバイダーを使用することを想定していることです。ページがサイトマップにないため、サイトマッププロバイダーを利用できません。そのため、サイトマップと現在のノードを手動で設定する必要があります。サイトマップ全体のサイズが大幅に大きくなるため、サイトマップにニュースページを含めないことを選択します。
まず、SiteMap.CurrentNodeプロパティではなく、動的サイトマッププロバイダーのカスタムThisNodeプロパティを使用します。
public static SiteMapNode ThisNode
{
get
{
if (_thisNode == null)
{
if (SiteMap.CurrentNode != null)
{
return SiteMap.CurrentNode;
}
else
{
return null;
}
}
else
{
return _thisNode;
}
}
set
{
_thisNode = value;
}
}
ニュースの詳細ページ(/news-and-events-detail.aspx)で、動的プロバイダーで作成されたユーティリティメソッドを呼び出します。
// Set the ThisNode property to the /news-and-events-list.aspx node.
// This will allow all sitemap driven controls and logic (such as navs, info bar, and dynamic links) to function since these detail pages are not in the sitemap.
DynamicSiteMapProviders.SetThisNodeToAlternateNode("/news-and-events-list.aspx");
これはユーティリティメソッドです。
/// <summary>
/// Sets the DynamicSiteMapProviders.ThisNode property to the node of specified URL.
/// </summary>
/// <param name="urlOfNodeToSetTo">The URL of the node to set from.</param>
public static void SetThisNodeToAlternateNode(string urlOfNodeToSetTo)
{
SiteMapDataSource siteMapDataSource = new SiteMapDataSource();
siteMapDataSource.SiteMapProvider = "Main";
DynamicSiteMapProviders.ThisNode = siteMapDataSource.Provider.FindSiteMapNode(urlOfNodeToSetTo);
}
ここで、ベースマスターページでDynamicSiteMapProviders.ThisNodeプロパティをリセットする必要があります。これは静的であり、次にアクセスするページで手動で設定されたノードを使用したくないためです。これは、ページのライフサイクルのOnUnload()イベントを利用して、ページのロジックとレンダリングの実行が完了したときに行います。上記のThisNodeプロパティのGet/Setのロジックを見てください。
// This ensures that DynamicSiteMapProviders.ThisNode is not set to the node of a previously viewed page.
// This is mainly for news and events pages that are not in the sitemap and are using the news and events listing page node as the current node.
protected override void OnUnload(EventArgs e)
{
DynamicSiteMapProviders.ThisNode = null;
base.OnUnload(e);
}