0

我的网站正在从数据库和 xml 数据动态创建站点地图。但是,这对于列出新闻文章的网站的一部分来说效果很好,因此决定不将新闻文章详细信息页面放在站点地图中。因此,如果您要单击新闻文章的标题(从站点地图中的列表页面),它会将您带到包含文章的页面,但该页面/网址不在站点地图中。

我在使用的母版页中有控件和逻辑

SiteMap.CurrentNode

本质上,在页面加载时,我想将 SiteMap.CurrentNode 更改为新闻文章列表页面(位于站点地图中)的节点。因此,基本上在此页面上运行的所有逻辑都会将该页面视为列表页面。我无论如何都找不到这样做。

此代码将为我提供我想要的节点,因为我知道它的密钥。

SiteMapDataSource siteMapDataSource1 = new SiteMapDataSource();
    siteMapDataSource1.SiteMapProvider = "Main";
    SiteMapNode newsListingPageNode = siteMapDataSource1.Provider.FindSiteMapNodeFromKey(siteMapKey);

所以基本上我希望我能做到这一点:

SiteMap.CurrentNode = newsListingPageNode;

但是不能设置 CurrentNode。

关于我如何做到这一点的任何建议?我很感激帮助。

4

2 回答 2

2

根据这篇文章,您可以为 SiteMapResolve 事件创建一个自定义处理程序,并且大概可以从中返回一个自定义节点。

于 2011-12-21T13:34:09.487 回答
0

Here is the solution I came up with, though it's a little too complexed for my liking. Remember the issue is the currently viewed page is not in the sitemap and navigation, controls, and other logic is expecting to use the sitemap provider. Since the page isn't in the sitemap, the sitemap provider is not available, thus the reason I have to manually set the sitemap and the current node. We choose not to have the news pages in the sitemap as it would significantly increase the overall size of the sitemap.

First, I use a custom ThisNode property of the dynamic sitemap provider rather than the SiteMap.CurrentNode property.

     public static SiteMapNode ThisNode
    {
        get
        {
            if (_thisNode == null)
            {
                if (SiteMap.CurrentNode != null)
                {
                    return SiteMap.CurrentNode;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return _thisNode;
            }
        }
        set
        {
            _thisNode = value;
        }
    }

On the News detail page (/news-and-events-detail.aspx) I call a utility method created in the dynamic provider.

    // 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");

This is the utility method:

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

Now in the base master page I have to reset the DynamicSiteMapProviders.ThisNode property since its static and I don't want the next page I visit to still use the manually set node. I do this when the page is done running logic and rendering by utilizing the OnUnload() event of the page life cycle. Look at the logic of Get/Set of the ThisNode property above.

// 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);
}
于 2012-01-30T14:40:35.093 回答