Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

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?

like image 564
AJM Avatar asked Jun 03 '10 09:06

AJM


2 Answers

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");
like image 99
Enrico Campidoglio Avatar answered Oct 23 '22 18:10

Enrico Campidoglio


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.

like image 1
scherand Avatar answered Oct 23 '22 16:10

scherand