MvcSiteMap 2.3 gives an object reference not set to an instance of an object

Feb 1, 2011 at 8:31 AM

Good day,

I'm trying to use the newest MvcSiteMap in MVC 3.0 but I get an error:  an object reference not set to an instance of an object.

I installed the NuGet package.  I double checked that everything is registered (as well as the web.config in the shared/views folder.

It gives an error on this line:  @Html.MvcSiteMap().Menu(SiteMap.Provider.FindSiteMapNodeFromKey("Home"), truefalse, 2); in the _Layout.cshtml file.

This is the stacktrace:

at MvcSiteMapProvider.Web.Html.MenuHelper.BuildModel(MvcSiteMapHtmlHelper helper, SiteMapNode startingNode, Boolean startingNodeInChildLevel, Boolean showStartingNode, Int32 maxDepth) in C:\Projects\Codeplex\TFS10\mvcsitemap\Branches\2.3.0\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:line 265  

at MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapHtmlHelper helper, String templateName, SiteMapNode startingNode, Boolean startingNodeInChildLevel, Boolean showStartingNode, Int32 maxDepth) in C:\Projects\Codeplex\TFS10\mvcsitemap\Branches\2.3.0\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:line 227  

at MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapHtmlHelper helper, SiteMapNode startingNode, Boolean startingNodeInChildLevel, Boolean showStartingNode, Int32 maxDepth) in C:\Projects\Codeplex\TFS10\mvcsitemap\Branches\2.3.0\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:line 122  

at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\Users\vanderwesthuizenr.GESSNET\Desktop\Fleet System\Gess.Fleet\Gess.Fleet\Views\Shared\_Layout.cshtml:line 30  

at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()  

at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()  

at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)  

at System.Web.WebPages.WebPageBase.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer)  

at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)  

at System.Web.WebPages.WebPageBase.Write(HelperResult result)  

at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)  

at System.Web.WebPages.WebPageBase.PopContext()  

at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)  

at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)  

at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)  

at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)

Can somebody please help me fix this?

Thank you.

Coordinator
Feb 4, 2011 at 9:34 AM

That's a feature I haven't pushed to NuGet yet :-) Here's how to fix:

Registering MvcSiteMapProvider HtmlHelper functions with Razor ViewEngine

In the Web.config file under the Views folder, add the following under the <system.web.webPages.razor> element:

<system.web.webPages.razor>
  <! -- ... -->
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <! -- ... --> 
      <add namespace="MvcSiteMapProvider.Web.Html" /> 
      <add namespace="MvcSiteMapProvider.Web.Html.Models" /> 
    </namespaces>
  </pages>
</system.web.webPages.razor>