-
Notifications
You must be signed in to change notification settings - Fork 218
Specifying Node Order
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 theMvcSiteMapNodeAttribute
but its purpose was to ensure that processing of the nodes occurred in the correct order so the parent node was added to theSiteMap
before the child node. This was sometimes necessary to make theParentKey
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 theOrder
property setting may no longer be strictly required.
Want to contribute? See our Contributing to MvcSiteMapProvider guide.
- Upgrading from v3 to v4
- Routing Basics
- Configuring MvcSiteMapProvider
- Defining Sitemap Nodes in XML
- Defining Sitemap Nodes using .NET Attributes
- Defining Sitemap Nodes using IDynamicNodeProvider
- HtmlHelper Extensions
- Controlling URL Behavior
- Using Action Filter Attributes
- Sitemaps XML Protocol Endpoint for Search Engines
- Using Custom Attributes on a Node
- Specifying Node Order
- Advanced Node Visibility
- Multiple Navigation Paths to a Single Page
- Multiple Sitemaps in One Application
- Security Trimming
Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.
- MvcSiteMapProvider 4.0 - A Test Drive
- MvcSiteMapProvider 4.0 - SEO Features Tutorial
- How to Make MvcSiteMapProvider Remember a User’s Position
- MvcSiteMapProvider 4.0 - Cache Configuration
- MvcSiteMapProvider 4.0 - Extending the Cache
- MvcSiteMapProvider 4.0 - Unit Testing with the SiteMaps Static Methods
- Debugging an MvcSiteMapProvider Configuration
- Converting from C# to Vb MvcSiteMapProvider
- ASP.NET MVC Menu using Site Map Provider & Bootstrap 3 Navbar
- ASP.NET MVC SiteMapPath using Site Map Provider & Bootstrap Breadcrumbs
- NightOwl888's MvcSiteMapProvider Demos - Filter for "MvcSiteMapProvider" to see the most relevant.
- MvcSiteMapProvider Tutorial and Examples
- MvcSiteMapProvider Tutorial 2 - Breadcrumbs
- Getting Started with MvcSiteMapProvider
- Inside the MvcSiteMapProvider - Part 1
- Inside the MvcSiteMapProvider - Part 2: Dynamic node providers
- Inside the MvcSiteMapProvider - Part 3: The ISiteMapVisibilityProvider
- Inside the MvcSiteMapProvider - Part 4: The IAclModule
- Inside the MvcSiteMapProvider - Part 5: The ISiteMapNodeUrlResolver
- Styling MvcSiteMapProvider with CSS
- Using MvcSiteMapProvider with Twitter Bootstrap
- ASP.NET MVC Menu using Site Map Provider & Bootstrap Navbar