使用ViewBag时出现RuntimeBinderException [英] RuntimeBinderException when using ViewBag

查看:77
本文介绍了使用ViewBag时出现RuntimeBinderException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于_Layout.cshtml中使用的Viewbag项目,我们正在获得RuntimeBinderException.我们在内存事件探查器中观察到了这些异常.它们不是致命的(一切正常),但很烦人,我们想清除它们.

We are getting RuntimeBinderException for the Viewbag items used in the _Layout.cshtml. 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在视图中设置.

ViewBag.Title is set in the view.

此外,我们还有ViewBag.WebAnalyticsParameters,它是在控制器操作中设置的,并在_Layout.cshtml中使用.

Also, we have ViewBag.WebAnalyticsParameters which is set in controller action and used in _Layout.cshtml.

当我们从_Layout代码中删除ViewBag.Title和ViewBag.WebAnalyticsParameters时,不会发生异常并被探查器应用程序捕获.

When we remove ViewBag.Title and ViewBag.WebAnalyticsParameters from the _Layout code, the exceptions don't occur and got caught by the profiler application.

请注意,异常仅在页面的初始加载时发生.刷新后,不会发生异常,这表明视图编译是我们遇到问题的地方.

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.

更新1:我创建了一个空的MVC 4应用程序,并再次收到此错误.您可以看到附件的图像.

Update 1: I've created an empty MVC 4 application and getting this error again. 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>

更新2:我已经将项目从MVC 4升级到了5.3.2.仍然会发生例外情况.

Update 2: I've upgraded the project from MVC 4 to 5.3.2. The exceptions still occur.

推荐答案

这些异常是由所使用的基础动态对象引起的..NET框架总是会抛出这些异常.

These exceptions are caused by the underlying dynamic object that is being used. The .NET framework somehow always throws these exceptions.

解决方案:在VS中,转到菜单工具/选项",在左侧选择顶级项目 Debugging ,然后选择 [v]启用仅我的代码".这样可以避免VS严重破坏框架内部引发的异常.

Solution: in VS go to menu Tools/Options, select top-level item Debugging on the left, and select [v] Enable Just My Code. This avoids VS from breaking rather uselessly on exceptions thrown deep inside the framework.

这篇关于使用ViewBag时出现RuntimeBinderException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆