无法加载文件或程序集“WebGrease”或其某个依赖项

40

运行我的MVC4 Web应用程序时,出现以下错误:

Could not load file or assembly 'WebGrease' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileLoadException: Could not load file or assembly 'WebGrease' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

我原来的 web.config 文件内容如下:

 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
      <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="1.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>
    </assemblyBinding>

参考了一些 SO 上的答案后,我进行了更改:

<dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>

但是它仍然没有起作用。

可能有什么问题呢?

编辑:

输入图像描述

编辑2:

安装包后再次出现以下错误:

Server Error in '/' Application.

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: Could not load file or assembly 'MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.

Source Error: 


Line 249:      <providers>
Line 250:        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
Line 251:        <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
Line 252:      </providers>
Line 253:    </membership>

汇编语言文件是否存在于磁盘上? - scheien
@scheien 如何检查? - C Sharper
请检查项目目录中的 /bin/debug,或者您可以检查项目中的引用。缺少的程序集应该有一个黄色图标。 - scheien
你可以尝试使用 NuGet 移除 Microsoft.AspNet.Web.Optimization 包吗?这也会删除 WebGrease。然后再重新安装 Microsoft.AspNet.Web.Optimization 包。 - scheien
@scheien 我可以从哪里卸载它? - C Sharper
显示剩余6条评论
8个回答

57

我有同样的问题。

通过执行以下操作解决:

1)运行“包管理器控制台”

2)在控制台中输入:'Install-Package Microsoft.AspNet.Web.Optimization'

这将解决所有不正确的依赖项。


3
在将WebGrease升级为NuGet包1.6.0后,绑定重定向未被更新。因此,重新安装Microsoft.AspNet.Web.Optimization对我来说是正确的解决方法。最终,我的web.config现在有了正确的条目:https://gist.github.com/jpolvora/8864750c4e3fd9f3d961 - Jone Polvora
1
请为我执行以下操作:update-package -reinstall Microsoft.AspNet.Web.Optimization - Brian Riley
1
没问题,谢谢..!! - Smit Patel

8

我必须运行以下命令:Update-Package -Reinstall Microsoft.AspNet.Web.Optimization

这会更新我的web.config文件和所有相关引用。


3

当您引用的 DLL 与您的 web.config 文件配置不同时,就会出现此问题。您需要重新安装该 DLL。


3

请检查web.config中runtime/assemblyBinding部分的内容。在安装Kendo UI包后,我发现了错误注册的dependentAssembly元素。我手动删除了重复项,问题消失了。


3

替换为:

<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />

针对:

 <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />

2

只需将WebGrease更新到1.6版本即可。

PM> Update-Package WebGrease -Version 1.6

0

我删除了项目的"bin"、"obj"文件夹,以及解决方案的"packages"文件夹,然后它就可以运行了。


0
我的情况是这样的:在我的开发机上运行良好,但在主机平台上出现了相同的“webgrease缺失”错误。
我的解决方案:
1. 从\bin目录中删除所有.dll文件
2. 从web.config中删除所有引用。
3. 当我刷新主页并得到“some-next.dll缺失”错误时,我将该some-next.dll复制到bin目录中,并为每个“新”的dll添加了web.config的引用。
注意:查看WebGrease.dll属性|详细信息选项卡-确保产品版本与<dependentAssembly>匹配。在我的情况下,是这样的:
<dependentAssembly>
  <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-1.5.1.25624" newVersion="1.6.5135.21930" />
</dependentAssembly>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接