【问题标题】:Getting a System.NullReferenceException in the _Layout.cshtml file of my ASP.NET MVC 5 Project [duplicate]在我的 ASP.NET MVC 5 项目的 _Layout.cshtml 文件中获取 System.NullReferenceException [重复]
【发布时间】:2021-07-12 23:57:40
【问题描述】:

我开始在我的项目的 _Layout.cshtml 文件中收到 System.NullReferenceException。它发生在第 16 行 - @Scripts.Render("~/bundles/modernizr")。我删除了捆绑包,错误刚刚出现在一个阵容中。所以我删除了所有捆绑包,但它仍然移动到上面的标题行。

以下是错误详情:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=App_Web_erjqyhfy
  StackTrace:
   at ASP._Page_Views_Shared__Layout_cshtml.Execute() in C:\My_Code\CompetitionTracker\CompetitionTracker\Views\Shared\_Layout.cshtml:line 16
   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.<>c__DisplayClass40_0.<RenderPageCore>b__0(TextWriter writer)
   at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
   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.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

这是我在 _Layout 中的头:

<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>@ViewBag.Title | @ConfigurationManager.AppSettings["appName"]</title>

    @Styles.Render("~/Content/css")

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/modernizr")
    <script src="~/Scripts/Site.js"></script>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script src="https://kit.fontawesome.com/92e259da1e.js" crossorigin="anonymous"></script>
</head>

当我删除导致异常的项目时,我最终会打开这个选项卡...

我不知所措!请帮忙!这可能是什么原因造成的!?我真的不认为它与错误抛出的实际行有任何关系!

【问题讨论】:

  • 删除部分页面,直到问题消失。当问题消失时,您删除的最后一件事就是原因。
  • @mjwills 我删除了所有捆绑包引用和标题行,现在我得到一个“源不可用”选项卡,该选项卡与相同的 NullReferenceException 一起打开
  • 删除所有内容。它失败了吗?现在添加代码,一次一行。哪条线导致问题?我的赌注是&lt;title&gt;@ViewBag.Title | @ConfigurationManager.AppSettings["appName"]&lt;/title&gt;。或者第 16 行是什么。
  • 当源不可用页面出现时,我仍然得到 NullReferenceException。所以我仍然收到错误。当我重新添加标题时,它会回到在该行抛出异常。
  • @JohnG,不,这不能解决我的问题。这是 99% 的 VB 而不是 MVC,也没有解决布局页面或 Bundles 或 ConfigurationManager 引用上的异常

标签: c# asp.net-mvc layout nullreferenceexception


【解决方案1】:

首先您将@using System.Configuration 命名空间添加到_Layout?

webconfig 中的第二个存在appName?

【讨论】:

  • 是的,我确实有 using 语句,并且 appName 在 webconfig...
  • 是否可以输入您的完整代码?
  • “完成”是什么意思?我无法输入我的整个项目,即数千行代码...如果您的意思是这样,我可以显示 using 语句和 appName 引用?
  • _Layout.chtml代码有几千行?
  • 使用_Layout的视图是否返回模型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2020-01-06
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
相关资源
最近更新 更多