asp.net-mvc Viewbag.Title 错误:无法找到编译动态表达式所需的一种或多种类型。你缺少参考吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22974183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:38:02  来源:igfitidea点击:

Viewbag.Title error: One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?

asp.net-mvcrazorasp.net-mvc-5

提问by Marius St?nescu

I have an ASP.NET MVC 5 web application. In every .cshtml view file i get the following error for Viewbag: One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?

我有一个 ASP.NET MVC 5 Web 应用程序。在每个 .cshtml 视图文件中,我收到Viewbag的以下错误:无法找到编译动态表达式所需的一种或多种类型。你缺少参考吗?

I have references to Microsoft.CSharp.dll and System.Core.dll.

我引用了 Microsoft.CSharp.dll 和 System.Core.dll。

Here is my root web.config file:

这是我的根 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="***" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="RouteDebugger:Disabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <globalization culture="ro-RO" uiCulture="ro" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNet.Identity.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNet.Identity.EntityFramework" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Here is the web.config from the Views folder:

这是 Views 文件夹中的 web.config:

<?xml version="1.0"?>

<configuration>
    <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
            <namespaces>
                <add namespace="System.Web.Mvc" />
                <add namespace="System.Web.Mvc.Ajax" />
                <add namespace="System.Web.Mvc.Html" />
                <add namespace="System.Web.Optimization"/>
                <add namespace="System.Web.Routing" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>

    <system.webServer>
        <handlers>
            <remove name="BlockViewHandler"/>
            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
    </system.webServer>
</configuration>

I can build the solution, and the web application works as expected, but I keep getting this error in the IDE and it's very annoying. I spent the entire day trying to solve this issue. Can someone please tell me how to fix it?

我可以构建解决方案,并且 Web 应用程序按预期工作,但是我在 IDE 中不断收到此错误,这很烦人。我花了一整天的时间试图解决这个问题。有人可以告诉我如何解决吗?

Edit:

编辑:

When everything works well, when I navigate to definition (F12) of the ViewBag, I get the Assembly System.Web.Mvc.dll, v5.1.0.0from Recipes.Web\Bin\System.Web.Mvc.dlland there are 4 using statements, one of which is System.Runtime.CompilerServices.

当一切正常时,当我导航到 ViewBag 的定义 (F12) 时,我得到了Assembly System.Web.Mvc.dll, v5.1.0.0fromRecipes.Web\Bin\System.Web.Mvc.dll并且有 4 个 using 语句,其中一个是System.Runtime.CompilerServices.

When the error appears, when I navigate to definition (F12) of the ViewBag, I get the same Assembly System.Web.Mvc.dll, v5.1.0.0from Recipes.Web\Bin\System.Web.Mvc.dll, BUT there are only 3 using statements, System.Runtime.CompilerServicesis gone.

当错误出现时,当我导航到 ViewBag 的定义 (F12) 时,我Assembly System.Web.Mvc.dll, v5.1.0.0从 中得到相同的结果Recipes.Web\Bin\System.Web.Mvc.dll,但是只有 3 个 using 语句System.Runtime.CompilerServices消失了。

Sometimes, the error doesn't appear, but if I close the solution and Visual Studio then reopen the solution, the error appears again. I don't even rebuild or do anything else.

有时,错误不会出现,但如果我关闭解决方案并 Visual Studio 然后重新打开解决方案,错误会再次出现。我什至不重建或做任何其他事情。

Why is this happening?

为什么会这样?

回答by algor77

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

以管理员身份运行 VS 2012 时我没有这个问题。

Otherwise, what worked for me:

否则,什么对我有用:

  1. 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>

  2. set copy local = trueproperties for System.Coreand Microsoft.CSharp

  1. 在根 web 配置中添加了作为推荐参考的正确程序集作为编译节点的子节点 <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>

  2. local = trueSystem.Core和设置复制属性Microsoft.CSharp

回答by Marlos

For reference Microsoft.CSharp, change the value of property Copy Localfrom False to True.

作为参考Microsoft.CSharp,将属性值Copy Local从 False更改为 True。

回答by jaybro

This happened to me after I made a code change on the cshtml page with a syntax error. After correcting the syntax error, the errors persisted. So, I closed the cshtml and re-compiled... success. I reopened the cshtml and the errors were no longer there.

在我对 cshtml 页面进行代码更改并出现语法错误后,这发生在我身上。更正语法错误后,错误仍然存​​在。所以,我关闭了 cshtml 并重新编译...成功。我重新打开了 cshtml,错误不再存在。

回答by Mikhail Tulubaev

I hope this will help someone. Works for me in VS 2013 with asp.net MVC 5 and .NET 4.6.1

我希望这会帮助某人。在 VS 2013 中使用 asp.net MVC 5 和 .NET 4.6.1 对我有用

Readding Microsoft.CSharpdid not fix the problem. Also, adding System.Coreto system.web/compilation/assemblies/at web.config did not fix too.

阅读Microsoft.CSharp并没有解决问题。此外,添加System.Coresystem.web/compilation/assemblies/web.config 也没有解决。

I overcame this only this adding System.Web.Mvcto system.web/compilation/assemblies/section:

我克服了这一点,仅此添加System.Web.Mvcsystem.web/compilation/assemblies/部分:

<compilation debug="true" targetFramework="4.6.1">
  <assemblies>
      <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
</compilation>

Rebuilded the solution and red line disappeared.

重建解决方案,红线消失。

回答by Christophe Verheyen

Add the following to Application_Start() method in global.asax

将以下内容添加到 global.asax 中的 Application_Start() 方法

ViewEngines.Engines.Add(new RazorViewEngine());

ViewEngines.Engines.Add(new RazorViewEngine());