由于Razor中的空引用,出现了RuntimeBinderException异常

3

因为这个原因,我刚刚注册了一个账户。

我在Visual Studio上创建了一个空的Web项目(没有MVC、Forms或Web API),在调试时遇到了这行代码:

@if (Session["GenCpN"] != null) { @Session["GenCpN"] }

在我的default.cshtml中,当我运行_GenLayout.cshtml布局文件时,遇到了以下异常:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled by user code
  HResult=-2146233088
  Message=Cannot perform runtime binding on a null reference
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at CallSite.Target(Closure , CallSite , Object , String , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
       at CallSite.Target(Closure , CallSite , Object , String , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
       at ASP._Page__GenLayout_cshtml.Execute() in C:\Users\PJ\OneDrive\Documents\Dev\BWC\WebApp\_GenLayout.cshtml:line 7
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors)
       at System.Web.WebPages.WebPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer)
       at System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer)
       at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
       at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content)
       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.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)
  InnerException: 

顺便提一下,如果有帮助的话,当我每个语句都使用一行时,Visual Studio会在闭合括号处中断。此外,我记得我的代码以前可以工作 - 在其他更改中 - 我在子文件夹的子文件夹中添加了一个新的web.config文件(以添加处理程序)。我已经删除了该文件,但仍然无法工作。
我想这需要由专业人士回答,但我感谢任何答案,
谢谢!
更新1:我已经还原了几个更改并删除了几个文件,但我仍然在这个文件中遇到这个错误。我还删除并重新编写了default.cshtml和_GenLayout.cshtml,但仍然没有收到任何消息。这对解决方案产生了不可见的影响。我发现了一个错误吗?
更新2:事实证明,一旦我删除引用自定义类的后续行,问题就会消失,但我仍然无法理解为什么会出现特定的错误。我要删除的行是:
<link rel="stylesheet" type="text/css" href="@App.uri("arg1", "arg2")" />

_GenLayout.cshtml:第7行是什么? - Mike Brind
@MikeBrind _GenLayout.cshtml 是 default.cshtml 的布局文件。我的错误,我刚刚编辑了问题。当然 default.cshtml 包含 @{ Layout = "~/_GenLayout.cshtml"; } - fpsychias
_GenLayout.cshtml 的第7行是什么? - Mike Brind
@MikeBrind @if (Session["GenCpN"] != null) { @Session["GenCpN"] } - fpsychias
1个回答

0

OP的“UPDATE 2”启发我在处理空的RuntimeBinderException时看到了错误提示之外的地方,我解决了我的问题。

与其担心异常堆栈跟踪中指示的行(它是正确的),寻找其他地方的null值(在堆栈跟踪所指示的行上面或下面)

我的情况很简单,在某些情况下,ViewBag.Title没有设置,因此当我尝试将其用于包含在元描述中时,它就是null


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接