MVC SitemapProvider:刷新(!)动态节点页面后的NullReferenceException

时间:2011-05-29 18:49:30

标签: asp.net-mvc sitemap mvcsitemapprovider

我在MVC3页面(.NET Framework 4.0)中使用ASP.NET MVC SiteMapProvider 3.0。我在开发期间从SiteMapProvider v2切换到3.0,但在v2中也有所描述的问题。

SiteMapProvider在Web.config中指定如下:

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

该页面使用主区域和管理区域,站点地图包含这两个区域的节点,包括一些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>

在主页面中,我正在使用

  

&lt;%:Html.MvcSiteMap()。菜单(false)%&gt;

使用以下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>

一般情况下一切正常,但我有一个很大的问题,我还无法解决这个问题: 如果我在浏览器中打开一个与DynamicNode相关的站点并暂停几分钟(或者如果我在后台重建VS中的解决方案)然后在浏览器中刷新页面,我总是会得到一个System.NullReferenceException以下源错误和堆栈跟踪:

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

这在Debug和Release模式下都会发生,甚至在我的远程Web服务器上都会发生。显然,这阻止了我发布项目。由于我没有任何可能导致这种情况的想法,我非常感谢任何帮助!

€dit 01.06.2011:我之前使用的是3.1.0 RC发布程序集,但现在将3.1.0 SVN分支附加到我的项目中以便能够调试异常。我发现方法

中发生异常
  

受保护的MvcSiteMapNode   GetSiteMapNodeFromXmlElement(的XElement   node,SiteMapNode parentNode)

defaultValue.Value 为空的后续行中(siteMapNode和defaultValue不为空):

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

我还发现,一旦发生错误,就无法再次请求底层控制器操作 - 不是通过在浏览器中刷新页面而不是打开新的浏览器窗口或选项卡。首先,您必须在站点地图层次结构的较高级别请求具有非动态节点的URL的操作,然后您可以再次请求动态子节点。它几乎看起来像一个缓存问题,或者某些sitemap对象被GC破坏,然后无法正确重新创建。

1 个答案:

答案 0 :(得分:1)

这应该在3.1版本中修复。如果没有,请在CodePlex网站上发布错误。