ASP.NET MVC SiteMap
I have been going insane trying to figure out why I can't get the ASP.NET MVCSiteMap to work.
I have downloaded the latest version from CodePlex (version 2.3) and set up the references as outlined on CodePlex. I downloaded the DisplayTemplates and set up a basic Mvc.sitemap file:
<?xml version="1.0" encoding="utf-8" ?>
<开发者_开发问答mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-2.0" enableLocalization="true">
<mvcSiteMapNode title="Dashboard" controller="Admin" action="Index" changeFrequency="Always" updatePriority="Normal">
<mvcSiteMapNode title="Users" controller="Users" action="Index" />
<mvcSiteMapNode title="Reports" controller="Reports" action="Index" />
</mvcSiteMapNode>
</mvcSiteMap>
When using the HTML Helper to generate a basic menu, I get a blank output:
<%: Html.MvcSiteMap().Menu() %>
In the MenuHelperModel.ascx DisplayTemplate, I am outputting the number of nodes:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl`1[ [MvcSiteMapProvider.Web.Html.Models.MenuHelperModel,MvcSiteMapProvider] ]" %>
<%@ Import Namespace="System.Web.Mvc.Html" %>
<%@ Import Namespace="MvcSiteMapProvider.Web.Html.Models" %>
<%: Model.Nodes.Count %>
<ul>
<% foreach (var node in Model.Nodes) { %>
<li><%=Html.DisplayFor(m => node)%>
<% if (node.Children.Any()) { %>
<%=Html.DisplayFor(m => node.Children)%>
<% } %>
</li>
<% } %>
</ul>
The resulting output is:
0
I cannot figure out what I am doing wrong. I don't get any errors, and I am using the Mvc.sitemap file provided by the CodePlex project site with some minor modifications for my actions/controllers.
NOTE: I have also downloaded the sample project, but it will not compile for me and the sitemap file being used is way too complicated for me to figure out at this early stage in my understanding of how to use this.
Any help is greatly appreciated.
The 2.3 version is ready for MVC3. I recompiled the fresh source, had the add a reference to MVC2 assemblies, change two code lines until I had a working MvcSitemap.dll
In your Mvc.sitemap I had to change the namespace to:
http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0
(note the -3.0 at the end instead of -2.0, in the latest version it is -4.0 at the end)
And make sure the name of the controller matches a controller where you test it otherwise the security mechanism will prevent anything showing.
Please note that the project has moved to Github the namespace is still referencing codeplex.com.
I had the same problem - Nodes count 0. I found a solution for my problem, I hope it helps you.
In App_Start -> RouteConfig.cs I had:
routes.MapRoute("Robots.txt",
"robots.txt",
new { controller = "Home", action = "Robots" },
namespaces: new string[] { "Mvc4AppNamespace.Controllers" });
I removed namespaces: new string[] { "Mvc4AppNamespace.Controllers" }
and it worked. In my case the problem was with namespaces.
精彩评论