我有一个ASP.NET MVC4项目,编译精细VS2010。为了部署目的,我运行一个Nant脚本,试图使用aspnet_compiler.exe预编译网站,但我一直运行到一个程序集引用问题与System.Web.WebPage
error CS1705: Assembly ‘System.Web.Mvc,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35’ uses ‘System.Web.WebPages,Version=2.0.0.0,PublicKeyToken=31bf3856ad364e35’ which has a higher version than referenced assembly ‘System.Web.WebPages,Version=1.0.0.0,PublicKeyToken=31bf3856ad364e35’
我还在我的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="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
和我的csproj有以下参考:
<Reference Include="System.Web.WebPages,PublicKeyToken=31bf3856ad364e35"> <Private>True</Private> <HintPath>..\packages\AspNetWebPages.Core.2.0.20126.16343\lib\net40\System.Web.WebPages.dll</HintPath> </Reference>
解决方法
我通过在Web.config中明确引用2.0程序集来解决这个问题。我想由于某种原因,ASP.NET编译器(在我的情况下,当运行MvcBuildViews),使用旧的1.0程序集,如果找到。
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- … --> <system.web> <!-- … --> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Web.Mvc,PublicKeyToken=31bf3856ad364e35,processorArchitecture=MSIL" /> <add assembly="System.Web.Abstractions,PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Routing,PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.WebPages,processorArchitecture=MSIL" /> </assemblies> </compilation> <!-- … --> </system.web> <!-- … --> </configuration>
BTW:看起来这个问题发生在我第一次添加第三方ASP.NET MVC 3.0组件后。该组件通过程序集绑定工作正常,但它可能是为什么ASP.NET编译器尝试首先加载1.0网页的原因。