SiteMapResolve Event
Collapse the table of content
Expand the table of content

SiteMap.SiteMapResolve Event

 

Occurs when the CurrentNode property is accessed.

Namespace:   System.Web
Assembly:  System.Web (in System.Web.dll)

public static event SiteMapResolveEventHandler SiteMapResolve

Subscribers attach a SiteMapResolveEventHandler object to the static SiteMapResolve event to receive notification when the CurrentNode property is accessed. This enables the user to implement custom logic when creating a SiteMapNode representation of the currently executing page without requiring a custom provider implementation.

If you subscribe to the SiteMapResolve event, you also subscribe to the SiteMapResolve event on the default site map provider.

The following code example demonstrates how to handle the SiteMapResolve event on an ASP.NET Web page to modify the target URLs that are displayed by a site navigation control, such as the SiteMapPath control. In this example, the current page is a post page in an online bulletin board or forum. In order to render more meaningful site navigation, the URLs of the nodes that are displayed by the navigation control are appended with context relevant query strings.

System_CAPS_noteNote

The ASP.NET site navigation infrastructure guards against infinite recursion, which provides a safeguard and minimizes the security risk that is associated with accessing the CurrentNode property from within the SiteMapResolveEventHandler class.

The following code belongs in the Global.asax file. The event handler will be attached only once for the application. The code recognizes whether the page implements the ISiteMapResolver interface. If the interface is implemented, the ExpandForumPaths function is called.

private void Page_Load(object sender, EventArgs e)
{
    // The ExpandForumPaths method is called to handle
    // the SiteMapResolve event.
    SiteMap.SiteMapResolve +=
      new SiteMapResolveEventHandler(this.ExpandForumPaths);
}

private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
{
    // The current node represents a Post page in a bulletin board forum.
    // Clone the current node and all of its relevant parents. This
    // returns a site map node that a developer can then
    // walk, modifying each node.Url property in turn.
    // Since the cloned nodes are separate from the underlying
    // site navigation structure, the fixups that are made do not
    // effect the overall site navigation structure.
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    // Obtain the recent IDs.
    int forumGroupID = GetMostRecentForumGroupID();
    int forumID = GetMostRecentForumID(forumGroupID);
    int postID = GetMostRecentPostID(forumID);

    // The current node, and its parents, can be modified to include
    // dynamic querystring information relevant to the currently
    // executing request.
    if (0 != postID)
    {
        tempNode.Url = tempNode.Url + "?PostID=" + postID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumID))
    {
        tempNode.Url = tempNode.Url + "?ForumID=" + forumID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumGroupID))
    {
        tempNode.Url = tempNode.Url + "?ForumGroupID=" + forumGroupID.ToString();
    }

    return currentNode;
}

The following code defines a separate interface. (In a Web site project, you can put this code in the App_Code folder.) The ISiteMapResolver interface defines the ExpandForumPaths method.

// These methods are just placeholders for the example.
// One option is to use the HttpContext or e.Context object
// to obtain the ID.
private int GetMostRecentForumGroupID()
{
    return 24;
}

private int GetMostRecentForumID(int forumGroupId)
{
    return 128;
}

private int GetMostRecentPostID(int forumId)
{
    return 317424;
}

The following code belongs with the page that is at least three nodes deep in the site-map structure. The page implements the ISiteMapResolver interface, which enables the ExpandForumPaths method to be called.

<asp:SiteMapPath
id="SiteMapPath1"
runat="server"
RenderCurrentNodeAsLink="true" />

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft