这个问题有很多解决方案,请阅读下面的所有答案,它们可能会帮助您解决问题。如果您找到了新的解决方法,请在您的答案中记录
我正在尝试将System.Web.Optimization添加到我的ASP.NET Web Forms解决方案中。 我通过NuGet Packages添加了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 中。
<runtime>
<legacyUnhandledExceptionPolicy enabled="1"/>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.5.1.25624" newVersion="1.5.2.14234"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
但是没有任何好运。
我注意到我的网站的Web config文件包含这一行:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
如果我用 替换它。 <configuration>
然后一切正常,我没有得到运行时错误。不幸的是,我需要xmlns。我的项目的其他组件依赖于它。
为什么Optimization会尝试加载旧版本,而schema指向v2.0?有没有办法强制它加载最新或唯一可用的WebGrease.dll?
在不更改的情况下还能尝试什么?
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> ?
非常感谢您所提供的任何帮助!
编辑: 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) 确认,问题出在
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
然而,我不明白为什么