ViewBag.Title error

2019-01-23 02:27发布

问题:

Working against the current RC2 - the template that is generated Razor views includes:

@{
    ViewBag.Title = "Details1";
    Layout = "/Views/Shared/_Public.cshtml";

}

With a red squiggly under ViewBag.Title and this compiler error:'

Error   4   One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? c:\Visual Studio 2010\Projects\myProj\Views\Webinar\Details1.cshtml 6   2   TTSTrain.Webinars.WebEntry

But the project builds and functions correctly. Is the error indicative of other problems that should be addressed?

回答1:

I got the same problem after I removed the targetFramework attribute from the <compilation> element in the Web.config file.

Once I restored it to

<compilation debug="true" targetFramework="4.0">

Everything worked fine again!



回答2:

I solved it in the following way:

First i noticed using gacutil (Global Assembly Cache Utility) that it contained two references to System.Core, one to version 4.0 and one to version 3.5. Apparently inside the razor views, even if in the project i had the correct reference to version 4.0, it was still using version 3.5 and that's why i was getting the error about the dynamic types. To check if that's your case open as administrator Visual Studio Command Prompt and execute:

gacutil -l System.Core

To remove the reference to the old version of System.Core i did the following steps:

- cd %systemroot%\assembly\

From here you may have more that one "gac" directory, so you will have to search within each to find your component. For me, it was within the "gac_MSIL" directory.

- cd gac_msil
- cd System.Core
- cd <assembly version number>__<public key token>
- erase *.* Say "y" to are you sure.
- cd ..
- rd <assembly version number>__<public key token>
- cd ..
- rd System.Core

After that I opened my solution again in visual studio and the error was gone, it references properly to System.Core 4.0 and I was not getting the dynamic errors anymore :)

I hope it will help you as well, Best, N.



回答3:

Similar to @Kaiser's answer, I experienced this problem as a result of having multiple System.Core assemblies in the GAC.

I chose not to delete the 3.5 assembly, however. Instead, in the Views web.config, I modified the configuration/system.web/compilation node as follows:

<compilation debug="true" targetFramework="4.0">
    <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </assemblies>
</compilation>

The important line is the last assembly node: it tells the Razor compiler which GAC assembly version to use.

Once I did this, all was well in my Razor views.



回答4:

I do not have this problem when running VS 2012 as administrator.

Otherwise, what worked for me:

in root web config have added as recommended reference to correct assembly as child of compilation node`

<system.web>
    <compilation debug="true" targetFramework="4.5">
        <assemblies>
            <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </assemblies>
    </compilation>
</system.web>

set copy local = true properties for System.Core and Microsoft.CSharp`



回答5:

Do you have a reference to Microsoft.CSharp and System.Core?

MVC Views (usually) get compiled dynamically when you access your site, not when you compile the application in VS. I imagine you will see issues when running the site. Just add the two references and you should be fine.



回答6:

By using Peters answer i managed to solve the issue with Html.EditorFor(m => m.xxx) underline errors in the chtml files. Althought the ViewBar error persisted. So i changed the web.config like this

<compilation debug="true" targetFramework="4.5.1">
  <assemblies>
    <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </assemblies>
</compilation>

Notice the Microsoft.Csharp line. The above did the trick and now the chtml editor is clear from the red underlines. Thanks Peter



回答7:

I had the exact same problem. By default, when you create an MVC3 app it sticks a web.debug.config and a web.release.config in the solution. When I got rid of those two items, the ViewBag issue resolved itself. It may have something to do with what Peter was saying above but I didn't test that.



回答8:

That is the time when other fields in ViewBag is read. So if you are passing them from controller. ViewBag.yourobjectDto = yourObjectDto;

make sure this line is not blocked through if condition or something.



回答9:

Try

Page.Title = "Details1";

It might work.