[英]RuntimeBinderException when using ViewBag
We are getting RuntimeBinderException for the Viewbag items used in the _Layout.cshtml. 我们正在为_Layout.cshtml中使用的Viewbag项获取RuntimeBinderException。 We observe these exceptions in the memory profiler. 我们在内存分析器中观察到这些异常。 They are not fatal (everything works OK) but annoying and we want to clear them out. 它们不是致命的(一切正常)但令人讨厌,我们想要清除它们。
For example, the following code causes an exception : 例如,以下代码导致异常:
<title>@ViewBag.Title</title>
...
@RenderBody()
ViewBag.Title is set in the view. ViewBag.Title在视图中设置。
Also, we have ViewBag.WebAnalyticsParameters which is set in controller action and used in _Layout.cshtml. 此外,我们有ViewBag.WebAnalyticsParameters,它在控制器操作中设置并在_Layout.cshtml中使用。
When we remove ViewBag.Title and ViewBag.WebAnalyticsParameters from the _Layout code, the exceptions don't occur and got caught by the profiler application. 当我们从_Layout代码中删除ViewBag.Title和ViewBag.WebAnalyticsParameters时,异常不会发生并被分析器应用程序捕获。
As a side note, the exceptions only occur at the initial loading of the page. 作为旁注,例外仅在页面的初始加载时发生。 After refreshing it, the exceptions don't occur which shows that the view compilation is the place we encounter problems. 刷新之后,不会发生异常,这表明视图编译是我们遇到问题的地方。
Update 1: I've created an empty MVC 4 application and getting this error again. 更新1:我创建了一个空的MVC 4应用程序并再次出现此错误。 You can see the attached image. 您可以看到附加的图像。
The view (Home/Index) in this sample app includes the following code: 此示例应用程序中的视图(主页/索引)包括以下代码:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
Update 2: I've upgraded the project from MVC 4 to 5.3.2. 更新2:我已将项目从MVC 4升级到5.3.2。 The exceptions still occur. 例外情况仍然存在。
These exceptions are caused by the underlying dynamic object that is being used. 这些异常是由正在使用的基础动态对象引起的。 The .NET framework somehow always throws these exceptions. .NET框架总是抛出这些异常。
Solution: in VS go to menu Tools/Options, select top-level item Debugging
on the left, and select [v] Enable Just My Code . 解决方案:在VS中,转到菜单工具/选项,选择左侧的顶级项目Debugging
,然后选择[v]启用我的代码 。 This avoids VS from breaking rather uselessly on exceptions thrown deep inside the framework. 这避免了VS在框架内部深处抛出的异常中无用地破坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.