In a simple Viewbag.Title, getting a RuntimeBinderException

Viewed 8238

I have a really simple ViewBag.Title. Like this:

@{
    ViewBag.Title = "My Title";
    ViewBag.MiniTitle = "Sub - Title";
}

Which is being parsed on _Layout.cshtml, on the

<title>@ViewBag.Title</title>

However, I am getting this exception:

Thrown: "'System.Dynamic.DynamicObject' does not contain a definition for 'Title'"
(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) 
Exception Message = "'System.Dynamic.DynamicObject' does not contain a definition for 
'Title'", Exception Type = "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"

I been searching and I could not find much information about it. It is basically the same issue this guy was facing: http://forums.asp.net/t/1715878.aspx?MVC3+Razor+Viewbag+Title+RuntimeBinderException

My issue is also the same as last guy that posted. This does not cause any problems for me, projects works fine and so does my titles. However, I am not liking the fact that an exception is being thrown due to the fact they are expensive.

Does anyone know how can i fix this issue? Thanks!

6 Answers

Your DLL is corrupt. I fixed it with me by copying Microsoft.CSharp.dll from my colleague. Location is C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7 (Last folder for version you adjust according to yours)

I was getting a similar issue. The only way I could fix it was by changing ViewBag.Title, for instance, to ViewData["Title"]. Technically, ViewBag and ViewData are the same thing and ViewData is faster to use but you have to type cast... But if it was a string anyway it appears to work fine with no cast since it'll call ToString automatically.

Using the Hot Reload function in VisualStudio 2022 can cause this error to occur seemingly random, without any problems in the code.

A fix is to stop running/debugging, build and restart.

I'm using VS2022 version 17.1.6 currently.

Related