MVC SitemapProvider: NullReferenceException after

2019-08-04 07:07发布

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:                 <div id="navi">
Zeile 30:                     <%--Navigation--%>
Zeile 31:                     <%: Html.MvcSiteMap().Menu(false)%>
Zeile 32:                 </div>
Zeile 33:             </div>

[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!

€dit 01.06.2011: I was previously using the 3.1.0 RC release assembly but now attached the 3.1.0 SVN branch to my project to be able to debug the exception. I found out that the exception occurs in the method

protected MvcSiteMapNode GetSiteMapNodeFromXmlElement(XElement node, SiteMapNode parentNode)

in the follwing lines where defaultValue.Value is null (siteMapNode and defaultValue aren't null):

Line 1379: if (siteMapNode[defaultValue.Key] == null)
Line 1380: {
Line 1381:     siteMapNode[defaultValue.Key] = defaultValue.Value.ToString();
Line 1382: }

I also found out that once the error occured, one can't request the underlying controller action again - not by refreshing the page in the browser and not by opening a new browser window or tab. You first have to request an action that has the URL of a non-dynamic node on an upper level in the sitemap hierarchy and then you can request the dynamic child node again. It almost seems like a caching problem or like some sitemap object is destroyed by the GC and then doesn't get recreated properly.

1条回答
\"骚年 ilove
2楼-- · 2019-08-04 07:50

This should be fixed with the 3.1 release. If not, please post a bug on the CodePlex site.

查看更多
登录 后发表回答