MVC SitemapProvider: NullReferenceException after refreshing(!) dynamic node pages

May 29, 2011 at 6:55 PM

Hi there,

I am using the ASP.NET MVC SiteMapProvider 3.0 in my MVC3 page (.NET Framework 4.0). I switched from SiteMapProvider v2 to 3.0 during development but had the described problem in v2 as well.

The SiteMapProvider is specified in the Web.config like this:

<siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
    <providers>
        <clear/>
        <add name="MvcSiteMapProvider" 
                type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider" 
                siteMapFile="~/Web.Sitemap" 
                securityTrimmingEnabled="false" 
                cacheDuration="5" 
                enableLocalization="false" 
                scanAssembliesForSiteMapNodes="true" 
                skipAssemblyScanOn="" 
                attributesToIgnore="bling,visibility" 
                nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider" 
                controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider" 
                actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider" 
                aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider" 
                siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider" 
                siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider" 
                siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider"/>
    </providers>
</siteMap>
The page uses a main and an administrative area and the sitemap contains nodes for both of those areas, including some DynamicNodes:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="false">
    <mvcSiteMapNode title="Startseite" controller="Home" action="Index" changeFrequency="Monthly" updatePriority="Normal">
        <!--Main Page-->
        <mvcSiteMapNode title="Produktkatalog" controller="Products" action="Index" >
            <mvcSiteMapNode title="Fahrr�der" controller="Bikes" action="List" key="bikes_" >
                <mvcSiteMapNode title="Marken" controller="Bikes" action="List" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.BikeBrandsDynamicNodeProvider, Grauthoff.WebUI">
                    <mvcSiteMapNode title="Details" controller="Bikes" action="Details" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.BikeDetailsDynamicNodeProvider, Grauthoff.WebUI" />
                </mvcSiteMapNode>
                <mvcSiteMapNode title="Kategorien" controller="Bikes" action="List" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.BikeCategoriesDynamicNodeProvider, Grauthoff.WebUI" />
            </mvcSiteMapNode>
            <mvcSiteMapNode title="Absperrpfosten" controller="Barriers" action="List" key="barriers_" >
                <mvcSiteMapNode title="Kategorien" controller="Barriers" action="List" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.BarrierCategoriesDynamicNodeProvider, Grauthoff.WebUI" />
            </mvcSiteMapNode>
            <mvcSiteMapNode title="Tresore" controller="Safes" action="List" key="safes_" >
                <mvcSiteMapNode title="Marken" controller="Safes" action="List" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.SafeBrandsDynamicNodeProvider, Grauthoff.WebUI">
                    <mvcSiteMapNode title="Details" controller="Safes" action="Details" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.SafeDetailsDynamicNodeProvider, Grauthoff.WebUI" />
                </mvcSiteMapNode>
                <mvcSiteMapNode title="Kategorien" controller="Safes" action="List" dynamicNodeProvider="Grauthoff.WebUI.Infrastructure.SafeCategoriesDynamicNodeProvider, Grauthoff.WebUI" />
            </mvcSiteMapNode>
        </mvcSiteMapNode>
        <mvcSiteMapNode title="Marken" controller="Brands" action="Index" />
        [...]
        <!--Admin Area-->
        <mvcSiteMapNode title="Start" controller="Home" action="Index" area="Admin"  />
            [...]    
</mvcSiteMap>

In the main page, I am using <%: Html.MvcSiteMap().Menu(false) %> to render the page navigation, using the following DisplayTemplate:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl`1[ [MvcSiteMapProvider.Web.Html.Models.MenuHelperModel,MvcSiteMapProvider] ]" %>

<%--this controls the output of the main navigation bar on the main page--%>
<ul id="MainNav">
<% foreach (var node in Model.Nodes) { %>
    <%--skip AdminArea nodes--%>
    <% if (node.Area.Equals("Admin")) { %>    
        <% continue; %>
    <% } %>
    <%--hightlight active node using a CSS class--%>
    <% if (node.IsCurrentNode || node.IsInCurrentPath || (SiteMap.CurrentNode != null && SiteMap.CurrentNode.Url.Equals(node.Url)))
       { %>    
        <li class="naviActive"><%=Html.DisplayFor(m => node)%>
    <% } %>
    <% else { %>
        <li class="naviInactive"><%=Html.DisplayFor(m => node)%>
    <% } %>   
    </li>
<% } %>
</ul>

Generally everything works fine but I have one big showstopper-kind-of problem that I couldn't solve yet: If I open one of the DynamicNode related sites in the browser and just stay idle for several minutes (or if I rebuild the solution in VS in the background) and then refresh the page in the browser, I always get a System.NullReferenceException with the following source error and stacktrace:

Zeile 29:                 
Zeile 33:             

[NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.]
   MvcSiteMapProvider.DefaultSiteMapProvider.GetSiteMapNodeFromXmlElement(XElement node, SiteMapNode parentNode) in D:\Downloads\smp\Main\src\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:1348
   MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() in D:\Downloads\smp\Main\src\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:483

[MvcSiteMapException: An error occured while building the sitemap... Check the InnerException for more details.]
   MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() in D:\Downloads\smp\Main\src\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:563
   MvcSiteMapProvider.DefaultSiteMapProvider.GetRootNodeCore() in D:\Downloads\smp\Main\src\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:131
   MvcSiteMapProvider.DefaultSiteMapProvider.get_RootNode() in D:\Downloads\smp\Main\src\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:103
   MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapHtmlHelper helper, Boolean showStartingNode) in D:\Downloads\smp\Main\src\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:45
   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\inetpub\vhosts\grauthoff-shop.de\httpdocs\Views\Shared\Site.Master:31
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +43
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060

This happens both in Debug and in Release mode and even on my remote webserver. Obviously, this prevents me from publishing the project. Since I don't have the slightest idea what might cause this, I am extremly thankful for any help!

Coordinator
May 31, 2011 at 11:42 AM

Can you check if this is also the case with 3.1?

May 31, 2011 at 9:15 PM
Edited Jun 6, 2011 at 8:57 AM

Unfortunately, I can confirm that the error still occurs in 3.1.0 RC.

I posted more details about the exception on Stackoverflow.

Coordinator
Jun 14, 2011 at 8:16 AM

Can you check with the latest sources?