我最近通过在 MVC4 中创建一个新的 Web 应用程序,将所有文件复制过来,最后合并 web.config,将我的 Web 应用程序从 MVC3 升级到 4。一切都适用于现有的网络应用程序,但是我今天刚刚注意到一个非常奇怪的错误 - 如果我尝试使用默认的虚拟实现创建一个新的 API 控制器,然后浏览到该 api(例如 /api/events),我得到一个Could not load type
错误。奇怪的是,如果我重新构建应用程序并再次点击 URL,它似乎是它抱怨的不同程序集,主要是在这两个之间交替:
Could not load type 'System.Web.Razor.Parser.SyntaxTree.CodeSpan' from assembly 'System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
和
Could not load file or assembly 'Microsoft.Scripting, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1' or one of its dependencies. The system cannot find the file specified.
我的 web.config 包含以下程序集绑定信息:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<!--<assemblyIdentity name="FluentValidation" publicKeyToken="a82054b837897c66" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="3.2.0.0" />-->
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-4.0.8.0" newVersion="4.0.8.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
而且我的 packages.config 看起来它包含所有正确的位:
<package id="AspNetMvc" version="4.0.20126.16343" />
<package id="AspNetRazor.Core" version="2.0.20126.16343" />
<package id="AspNetWebApi" version="4.0.20126.16343" />
<package id="AspNetWebApi.Core" version="4.0.20126.16343" />
<package id="AspNetWebPages.Core" version="2.0.20126.16343" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
<package id="Microsoft.Web.Optimization" version="1.0.0-beta" />
<package id="System.Net.Http" version="2.0.20126.16343" />
<package id="System.Net.Http.Formatting" version="4.0.20126.16343" />
<package id="System.Web.Http.Common" version="4.0.20126.16343" />
<package id="System.Web.Providers" version="1.1" />
<package id="System.Web.Providers.Core" version="1.0" />
我什至尝试使用新的 mvc4 Web 应用程序中的“香草”复制我的 web.config,但仍然给我同样的错误(无法加载文件或程序集 Microsoft.Scripting)