这个问题有很多解决方案,请阅读下面的所有答案,它们也可以帮助您解决问题.如果您找到解决此问题的新方法,请在答案中记录
我试图将System.Web.Optimization添加到我的ASP.NET Web窗体解决方案.我通过NuGet包添加了Microsoft ASP.NET Web Optimization Framework.它将Microsoft.Web.Infrastracture和WebGrease(1.5.2)添加到引用中.
但是,当我跑
<%= System.Web.Optimization.Scripts.Render("~/bundles/js")%>
我得到运行时错误
Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
我已经尝试将assemblyBinding添加到Web.Config
但没有任何运气.
我注意到我的WebSite的Web配置包含这一行
如果我用它替换它
然后一切正常,我没有得到运行时错误.不幸的是,我需要xmlns.我项目的其他组件依赖于它.
为什么当架构指向v2.0时,Optimization会尝试加载旧版本?有没有办法强制它加载最新或唯一可用的WebGrease.dll?
如果不改变,我还能尝试什么
?
感谢您提供任何帮助!
编辑:1)附加FusionLog结果.也许它会有所帮助
=== Pre-bind state information === LOG: User = [USER] LOG: DisplayName = WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///C:/Projects/PROJECT_NAME/trunk/www.PROJECT_NAME.com/ LOG: Initial PrivatePath = C:\Projects\PROJECT_NAME\trunk\www.PROJECT_NAME.com\bin Calling assembly : System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Projects\PROJECT_NAME\trunk\www.PROJECT_NAME.com\web.config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35
2)确认,问题在于
但是,我不明白为什么
我修改了我的web.config文件,以便newVersion ="1.0.0.0"与我的Referenced文件版本匹配:
<dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.0.0.0" /> </dependentAssembly>
最后,问题出在了<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
.它导致Render方法加载错误的WebGrease程序集.
删除xmlns解决了我的问题.
我在prod服务器上遇到了这个问题,而在开发人员机器上一切正常.这些行帮助:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.0" newVersion="1.5.2.14234"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>