Reputation: 2190
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 is set in the view.
Also, we have ViewBag.WebAnalyticsParameters which is set in controller action and used in _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.
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. 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. The exceptions still occur.
Upvotes: 5
Views: 3075
Reputation: 24147
These exceptions are caused by the underlying dynamic object that is being used. The .NET framework somehow always throws these exceptions.
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.
Upvotes: 4