Skip to content

Specifying Node Order

Shad Storhaug edited this page Apr 24, 2018 · 2 revisions

As of version 4.3.0, you can set the order that the nodes will appear in the @Html.MvcSiteMap().Menu() and @Html.MvcSiteMap().SiteMap() HTML helpers. This can be done by setting the Order property or attribute of the node to an integer value. The lowest value will appear first below its parent node in the HTML helper, and the highest value will appear last. That is, the order is only relative to the other nodes that have the same parent node (the sibling nodes). If no order is specified on a node, the default is 0.

Node ordering can come in handy when you have nodes on a certain level that are configured from different sources. In this example, we demonstrate how you can add some nodes via XML, some via MvcSiteMapNodeAttribute, and some via a dynamic node provider and order them appropriately.

<mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
    <mvcSiteMapNode title="Dynamic Node Stub" dynamicNodeProvider="MyNamespace.MyOrderedDynamicNodeProvider, MyAssemblyName"/>
    <mvcSiteMapNode title="Node 6" controller="Home" action="Node6" order="6"/>
    <mvcSiteMapNode title="Node 3" controller="Home" action="Node3" order="3"/>
    <mvcSiteMapNode title="Node 8" controller="Home" action="Node8" order="8"/>
</mvcSiteMapNode>
public class MyOrderedDynamicNodeProvider
    : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        var nodes = new List<DynamicNode>();

        var node9 = new DynamicNode("Node9", "Home", "Node 9", "", "Node9");
        node9.Order = 9;
        nodes.Add(node9);
		
        var node2 = new DynamicNode("Node2", "Home", "Node 2", "", "Node2");
        node2.Order = 2;
        nodes.Add(node2);

        var node5 = new DynamicNode("Node5", "Home", "Node 5", "", "Node5");
        node5.Order = 5;
        nodes.Add(node5);

        return nodes;
    }
}
public class HomeController : Controller
{       
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

    [MvcSiteMapNode(Title = "Node 1", ParentKey = "Home", Key = "Node1", Order = 1)]
    public ActionResult Node1
    {
        ViewBag.Message = "Node 1 page.";
			
        return View();
    }

    public ActionResult Node2
    {
        ViewBag.Message = "Node 2 page.";
			
        return View();
    }
	
    public ActionResult Node3
    {
        ViewBag.Message = "Node 3 page.";
			
        return View();
    }

    [MvcSiteMapNode(Title = "Node 4", ParentKey = "Home", Key = "Node4", Order = 4)]
    public ActionResult Node4
    {
        ViewBag.Message = "Node 4 page.";
			
        return View();
    }
	
    public ActionResult Node5
    {
        ViewBag.Message = "Node 5 page.";
			
        return View();
    }
	
    public ActionResult Node6
    {
        ViewBag.Message = "Node 6 page.";
			
        return View();
    }

    [MvcSiteMapNode(Title = "Node 7", ParentKey = "Home", Key = "Node7", Order = 7)]
    public ActionResult Node7
    {
        ViewBag.Message = "Node 7 page.";
			
        return View();
    }
	
    public ActionResult Node8
    {
        ViewBag.Message = "Node 8 page.";
			
        return View();
    }
	
    public ActionResult Node9
    {
        ViewBag.Message = "Node 9 page.";
			
        return View();
    }
}

If you were to put the above configuration into a project and add a @Html.MvcSiteMap().SiteMap() HTML helper to your /Views/Shared/_Layout.cshtml view, you would see the nodes listed in order.

  • Home
    • Node 1
    • Node 2
    • Node 3
    • Node 4
    • Node 5
    • Node 6
    • Node 7
    • Node 8
    • Node 9

Note: In versions prior to 4.3.0, the Order property existed on the MvcSiteMapNodeAttribute but its purpose was to ensure that processing of the nodes occurred in the correct order so the parent node was added to the SiteMap before the child node. This was sometimes necessary to make the ParentKey match. Although this property has been re-purposed in 4.3.0, it is safe to upgrade an older configuration without affecting the node order even though the Order property setting may no longer be strictly required.


Want to contribute? See our Contributing to MvcSiteMapProvider guide.



Version 3.x Documentation


Unofficial Documentation and Resources

Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.

Tutorials and Demos

Version 4.x
Version 3.x

Forums and Q & A Sites

Other Blog Posts

Clone this wiki locally