This is a migrated thread and some comments may be shown as answers.

CurrentNode from SiteMapBase

5 Answers 96 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Derek Hunziker
Top achievements
Rank 1
Derek Hunziker asked on 09 Jul 2010, 04:41 PM
Hello,

How does one get the current node from the Telerik MVC sitemap? I only see a reference to the RootNode and all of it's children. I'm trying to build a breadcrumbs navigation menu but I can't get a handle on how to extract the current node.

Do I need to iterate through every node and check the combination of view/controller/action/area against the current request?

Thanks!

-Derek

5 Answers, 1 is accepted

Sort by
0
Derek Hunziker
Top achievements
Rank 1
answered on 13 Jul 2010, 04:58 AM
Sorry to bump this but does anyone know how to get the CurrentNode (i.e. current view page) from the sitemap?

There is another really great MVC sitemap project on codeplex that offers the breadcrumb helpers that I'm shooting for, but it's not compatable with Telerik's MVC extensions (as far as I can tell). I'm trying to create my own breadcrumbs helpers, but in order to do this, I need to get the current node.

Thanks!

0
Accepted
Atanas Korchev
Telerik team
answered on 13 Jul 2010, 06:32 AM
Hello Derek Hunziker,

Currently the SiteMap does not support this feature. I have logged it as a feature request and you can cast your vote for it.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Shane Milton
Top achievements
Rank 2
answered on 28 Jul 2010, 09:09 PM
Derek,

Does the .HighlightPath(true) feature not help you?

-Shane
0
Derek Hunziker
Top achievements
Rank 1
answered on 28 Jul 2010, 10:55 PM
Well, not exactly, but thanks very much for letting me know about that feature :).

What I'm after is a secondary navigation that starts at the current node. This is a pretty common requirement and very easy to build (It's even possible with the codeplex mvc sitemap project), but it requires that you know the current node.

The code below is as close as I could get (it's is my BaseController class that I inherit all of my controllers from)

[PopulateSiteMap(SiteMapName = "siteMap", ViewDataKey = "siteMap")]
public abstract class BaseController : Controller
{
    public BaseController()
    {
        // Add global contoller logic here
    }
 
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        SiteMapBase siteMap = SiteMapManager.SiteMaps["siteMap"];
        ViewData["currentNode"] = GetCurrentNode(siteMap.RootNode); // Store in ViewData
        base.OnActionExecuted(filterContext);
    }
 
    private SiteMapNode GetCurrentNode(SiteMapNode node)
    {
        // Compare the node's url to the current url
        if (this.Url.Action(node.ActionName, node.ControllerName, node.RouteValues) ==
            this.Request.Path)
            return node; // Match!
 
        // Recurse through childnodes
        foreach (SiteMapNode child in node.ChildNodes)
        {
            SiteMapNode match = GetCurrentNode(child);
            if (match != null)
                return match;
        }
 
        // Not found!
        return null;
    }
}


0
Derek Hunziker
Top achievements
Rank 1
answered on 11 Aug 2010, 11:20 PM
I ran into an issue today with my custom navigation and I thought I would share my solution.

For some reason when calling myNode.ChildNodes, all of the child nodes get returned including hidden nodes [nodes with "Visible" set to false] and nodes that are supposed to be invisible to the anonymous users [nodes with an Authorized attribute]. Since I only want to show navigation items that my users can actually navigate to, I needed to somehow check if each node was visible and accessible before adding it to the navigation.

Luckily, a Telerik MVC SiteMapNode inherits from INavigatable which allows you to do this:

var navAuthorization = Telerik.Web.Mvc.Infrastructure.ServiceLocator.Current.Resolve<Telerik.Web.Mvc.Infrastructure.INavigationItemAuthorization>();
 
if (node.Visible && navAuthorization.IsAccessibleToUser(helper.ViewContext.RequestContext, node)
{
    // Okay to show in navigation...
}
Tags
General Discussions
Asked by
Derek Hunziker
Top achievements
Rank 1
Answers by
Derek Hunziker
Top achievements
Rank 1
Atanas Korchev
Telerik team
Shane Milton
Top achievements
Rank 2
Share this question
or