AJM
AJM

Reputation: 32490

ASP.NET SiteMap - Is there a way to programatically see if it contains a page without iterating through each node individually

Want to check that my site map contains a page.

Could just iterate through SiteMap.RootNode.GetAllNodes() but is there a way to search for a page without iterating manually?

Upvotes: 5

Views: 791

Answers (2)

scherand
scherand

Reputation: 2358

If you are on .NET 2.0 you can do something similar: put your Nodes into a (generic) list and use Find(...). Along the lines of:

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 have to iterate manually :) If this is "better" is another question.

Upvotes: 1

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59983

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

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

Upvotes: 4

Related Questions