提问人:Alyson 提问时间:7/13/2021 最后编辑:urmat abdykerimovAlyson 更新时间:7/14/2021 访问量:2344
在我的 ASP.NET MVC 5 项目的 _Layout.cshtml 文件中获取 System.NullReferenceException [副本]
Getting a System.NullReferenceException in the _Layout.cshtml file of my ASP.NET MVC 5 Project [duplicate]
问:
我开始在项目的 _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>
当我删除导致异常的项目时,我最终会得到这个打开的选项卡......
我不知所措!请帮忙!是什么原因导致了这种情况!?我真的不认为这与抛出错误的实际行有任何关系!
答:
0赞
Meysam Asadi
7/13/2021
#1
首先,您将命名空间添加到_Layout?@using System.Configuration
第二个在webconfig中存在吗?appName
评论
0赞
Alyson
7/13/2021
是的,我确实有 using 语句,并且 appName 在 webconfig 中......
0赞
Meysam Asadi
7/13/2021
可以输入您的完整代码吗?
0赞
Alyson
7/13/2021
“完整”是什么意思?我无法输入我的整个项目,那是数千行代码......如果这是你的意思,我可以显示 using 语句和 appName 引用吗?
0赞
Meysam Asadi
7/13/2021
_Layout.chtml 代码是数千行?
0赞
Meysam Asadi
7/13/2021
使用 _Layout 的视图是否返回模型?
评论
<title>@ViewBag.Title | @ConfigurationManager.AppSettings["appName"]</title>