ASP.NET SiteMap - is there a way to programmatically see if it contains a page without repeating through each node individually

Want to verify that my site map contains a page.

You can simply iterate through SiteMap.RootNode.GetAllNodes (), but is there a way to search for a page without iterating manually?

+5
source share
2 answers

If you are using the .NET Framework 3.5, you can use the LINQ method:

SiteMapNodeCollection pages = SiteMap.RootNode.GetAllNodes();
SiteMapNode myPage = pages.SingleOrDefault(page => page.Url == "somePageUrl");
+4
source

If you are on .NET 2.0, you can do something similar: put your Nodes in the (common) list and use Find(...). Line by line:

string urlToLookFor = "myPageURL";
List<SiteMapNode> myListOfNodes = new
        List<SiteMapNode>(SiteMap.RootNode.GetAllNodes());
SiteMapNode foundNode = myListOfNodes.Find(delegate(SiteMapNode currentNode)
{
    return currentNode.Url.ToString().Equals(urlToLookFor);
});

if(foundNode != null) {
    ... // Node exists
}

This way you do not need to iterate manually. If it is “better,” this is another question.

+1
source

All Articles