【问题标题】:Viewbag.Title error: One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?Viewbag.Title 错误:找不到编译动态表达式所需的一种或多种类型。您是否缺少参考?
【发布时间】:2014-05-23 08:12:21
【问题描述】:

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

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

这是我的根 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>

这是 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>

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

编辑:

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

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

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

为什么会这样?

【问题讨论】:

  • 您是否尝试升级您的项目
  • 升级是什么意思?我拥有最新的所有 nuget 包(aspnet.mvc - 5.1.2、aspnet.razor - 3.1.2、aspnet.webpages - 3.1.2)。我有一种预感,这可能是错误的原因。在更新 nuget 包、IDE 和扩展并对应用程序进行一些更改之前,一切正常。
  • 我最终撤销了最后一次提交,错误消失了。可悲的是,我不得不再次做出改变。我不知道出了什么问题以及导致错误的原因。
  • 现在又发生了......我不知道为什么!
  • 觉得你可以试试stackoverflow.com/questions/4605263/viewbag-title-error中的任何一个

标签: asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

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

【讨论】:

  • 这对我有用,并解决了另一个模拟问题
  • 这为我解决了问题。谢谢一百万!
  • 有人能把需要更改的 web.config 中的那行放在一边吗?
【解决方案2】:

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

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

【讨论】:

  • 我有一个自定义的 Razor 视图引擎,它继承了 BuildManagerViewEngine。我在 global.asax 中有这个:ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new CustomRazorViewEngine());
  • 问题还是出现?您的 CustomRazorViewEngine 继承自 RazorViewEngine,因此如果这没有帮助,则不是您问题的解决方案
  • 这就是解决我项目问题的方法。我将以下内容添加到 web.config &lt;system.web&gt; &lt;compilation debug="true" targetFramework="4.5"&gt; &lt;assemblies&gt; &lt;add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt; &lt;/assemblies&gt; &lt;/system.web&gt;
  • 它是 的孩子!
【解决方案3】:

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

否则,什么对我有用:

  1. 在根 web 配置中添加了作为编译节点子级的正确程序集的推荐参考 &lt;system.web&gt; &lt;compilation debug="true" targetFramework="4.5"&gt; &lt;assemblies&gt; &lt;add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt; &lt;/assemblies&gt;&lt;/compilation&gt; &lt;/system.web&gt;

  2. System.CoreMicrosoft.CSharp设置复制local = true属性

【讨论】:

  • 你的意思是 Microsoft.CSharp... 因为我没有找到 system.Csharp 来参考
  • 谢谢。这也有助于解决我在 Razor 中看到的一些 Linq 问题。我曾尝试从 GAC 中删除 3.5 System.Core。无论如何,这最后一根稻草救了船。
  • 有人可以将需要更改为local = true case 的web.config 中的行放入吗? ——
  • 刚刚添加了 targetFramework 行。 ViewBag 已解决。
  • @Unbreakable - 在你的VS项目中进入References,选择你想设置为本地的引用,右击并选择Properties,然后你会看到Copy Local设置,将它设置为true。
【解决方案4】:

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

【讨论】:

  • 这正是发生在我身上的事情。谢谢。
【解决方案5】:

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

阅读Microsoft.CSharp 并没有解决问题。此外,在 web.config 中将 System.Core 添加到 system.web/compilation/assemblies/ 也没有修复。

我克服了这个问题,只是将System.Web.Mvc 添加到system.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>

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

【讨论】:

  • 对我来说 (Mvc 4 + .NET 4.5.1) 删除 System.Web.Mvc 引用并手动重新添加就足够了。如果你使用 nuget,它在 ~/packages 文件夹中。
猜你喜欢
  • 2014-08-25
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-01
  • 1970-01-01
相关资源
最近更新 更多