Want to check that my site map contain开发者_运维问答s 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.
精彩评论