开发者

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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜