NullReferenceException in MvcSiteMapProvider at pr

2019-06-13 18:50发布

问题:

The problem is I regularly get the error at the production site (ASP.NET MVC 3) but can't reproduce this error locally. The text of exception is:

ExceptionType: System.NullReferenceException
Message: Object reference not set to an instance of an object.
StackTrace:
   at System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllKeys()
   at System.Collections.Specialized.NameValueCollection.get_AllKeys()
   at MvcSiteMapProvider.MvcSiteMapNode.get_MetaAttributes()
   at MvcSiteMapProvider.Web.Html.SiteMapNodeModelMapper.MapToSiteMapNodeModel(SiteMapNode node, MvcSiteMapNode mvcNode, IDictionary`2 sourceMetadata)
   at MvcSiteMapProvider.Web.Html.SiteMapPathHelper.BuildModel(MvcSiteMapHtmlHelper helper, SiteMapNode startingNode)
   at MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper, String templateName)
   at ASP._Page_Views_Shared__LoggedInLayout_cshtml.Execute() in c:\Solution\Sites\Project2Site\Views\Shared\_LoggedInLayout.cshtml:line 86
   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.Write(HelperResult result)
   at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
   at System.Web.WebPages.WebPageBase.PopContext()
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

I have no such errors locally. Here is where I invoke MvcSiteMap().SiteMapPath() method:

May be somebody had the same problem before. If so please share your solution.

Thanks in advance.

回答1:

This won't answer your question directly, but may help you on your path to debugging the issue, seeing as it happens only in production and not in dev/debug.

Consider replacing your line:

@Html.MvcSiteMap().SiteMapPath()

With something like:

@SafeBreadCrumb()

@helper SafeBreadCrumb() {
    MvcHtmlString output;
    try
    {
        output = Html.MvcSiteMap().SiteMapPath();
    }
    catch (Exception e)
    {
        output = new MvcHtmlString(e.Message);    
    }
    <text>@output</text>
}

Feels a little dirty, but it's easier than grabbing the source of MvcSiteMapProvider and sticking in some logging! If it happened in debug, you wouldn't need to try this. Plus it's just a view change, so won't need a recompile.