本文介绍了在NullReferenceException异常在MvcSiteMapProvider生产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我经常得到在生产现场(ASP.NET MVC 3)错误,但无法在本地重现此错误。
例外的文本是:

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)

我在本地没有这样的错误。这里是我祈求 MvcSiteMap()的SiteMapPath()方法:

可能是有人收到了同样的问题。如果是的话,请分享你的解决方案。

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

先谢谢了。

推荐答案

这不会直接回答你的问题,但因为它发生只在生产,而不是在开发可以帮助你在道路上调试问题,眼看/调试。

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.

考虑更换行:

@Html.MvcSiteMap().SiteMapPath()

使用类似:

@SafeBreadCrumb()

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

感觉有点脏,但它比抓住MvcSiteMapProvider的来源,并在一些伐木坚持更轻松!如果在调试发生了,你就不需要尝试。再加上它只是一个视图改变,所以就不需要重新编译。

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.

这篇关于在NullReferenceException异常在MvcSiteMapProvider生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:44