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?
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With