C# 无法加载文件或程序集“WebGrease,版本=1.5.1.25624,文化=中性,PublicKeyToken=31bf3856ad364e35”或其依赖项之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19851912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:00:24  来源:igfitidea点击:

Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies

c#.netasp.net-mvc-4dllwebgrease

提问by Jidheesh Rajan

I have an MVC4 Web API project. While running the service project I am getting an error

我有一个 MVC4 Web API 项目。运行服务项目时出现错误

Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

无法加载文件或程序集“WebGrease,版本=1.5.1.25624,文化=中性,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。该系统找不到指定的文件。

I have added the WebGrease.dll file and still its coming error

我已经添加了 WebGrease.dll 文件,但仍然出现错误

And tried with changing web.config

并尝试更改 web.config

<bindingRedirect oldVersion="0.0.0.0-1.3.0.0"/>"

And

<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />

采纳答案by Fernando Vezzali

Have you tried through NuGet ?

您是否尝试过 NuGet ?

Install-Package WebGrease -Version 1.5.1

or

或者

Install-Package WebGrease -Version 1.5.2

回答by c0y0teX

I know it's kind of late for the OP but I ran into the same problem while trying out the Bootstrap 3 for MVC 4NuGet package, in my case it had something to do with the Microsoft.AspNet.Web.Optimizationpackage, and managed to find a simple solution.

我知道 OP 有点晚了,但是我在尝试Bootstrap 3 for MVC 4NuGet 包时遇到了同样的问题,就我而言,它与Microsoft.AspNet.Web.Optimization包有关,并设法找到一个简单的解决方案。

Try executing the following commands in the package manager console:

尝试在包管理器控制台中执行以下命令:

Install-Package Microsoft.AspNet.Web.Optimization
Update-Package WebGrease
Uninstall-Package Microsoft.AspNet.Web.Optimization
Uninstall-Package WebGrease
Install-Package Microsoft.AspNet.Web.Optimization
Update-Package WebGrease

The first two lines had no effect for me since those packages were already installed and updated by the Bootstrap 3 for MVC 4package, but I ran them anyway and then it all compiled and ran great.

前两行对我没有影响,因为这些包已经由Bootstrap 3 for MVC 4包安装和更新,但我还是运行了它们,然后它全部编译并运行良好。

回答by rjchicago

The other answers did not resolve this for me. We are adding a newer MVC 5 API project and it wasn't playing nice with the older MVC 2 API's. After running package updates I was getting the mentioned error. The resolution for me was to remove the WebGrease references added by NuGet in the web.config's of the offending projects.

其他答案并没有为我解决这个问题。我们正在添加一个较新的 MVC 5 API 项目,但它与较旧的 MVC 2 API 并不兼容。运行包更新后,我收到了提到的错误。我的解决方案是删除 NuGet 在违规项目的 web.config 中添加的 WebGrease 引用。

To Resolve:Open the web.config of the project(s) throwing the exception and delete or comment out the dependency added for WebGrease.

解决方法:打开抛出异常的项目的web.config,删除或注释掉为WebGrease添加的依赖项。

回答by faradzen

Alreasy resolved this error. Situation was simpler than i thought. When you install from nuget new version of System.Web.Optimization it has reference to

已经解决了这个错误。情况比我想象的要简单。当您从 nuget 新版本的 System.Web.Optimization 安装时,它参考了

// References: WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35

// 参考:WebGrease,版本=1.5.1.25624,Culture=neutral,PublicKeyToken=31bf3856ad364e35

At same time i have WebGrease version 1.5.2.14234. Running project, we have this error: Could not load file or assembly 'WebGrease, Version=1.5.1.25624

同时我有 WebGrease 版本 1.5.2.14234。运行项目,我们有这个错误:无法加载文件或程序集'WebGrease,版本= 1.5.1.25624

Soo, i try to check this version on nuget and was surprized, that it wasn't...but when i create new project with VS - i got it. To hack this error i try to install it in GAC..but gac says me, that this lib is not subscribed. Downgrading to System.Web.Optimization Version=1.1.0.0 , where reference is:

Soo,我尝试在 nuget 上检查此版本并感到惊讶,它不是......但是当我用 VS 创建新项目时 - 我明白了。为了解决这个错误,我尝试在 GAC 中安装它。但是 gac 告诉我,这个库没有被订阅。降级到 System.Web.Optimization Version=1.1.0.0 ,其中参考是:

// References: WebGrease, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

// 参考:WebGrease,版本=1.3.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35

and 1.3.0.0 is strongly subscribed - helped me. It works. After upgrading webgrease, i have it Version=1.6.5135.21930 and it worked perfectly.

和 1.3.0.0 被强烈订阅 - 帮助了我。有用。升级 webgrease 后,我有它 Version=1.6.5135.21930 并且运行良好。

At conclusion, i have an opinion, than this error (Could not load file or assembly 'WebGrease, Version=1.5.1.25624) was because of that System.Web.Optimization lib is referenced to unsubscribed webgrease dll version.

最后,我有一个意见,比这个错误(无法加载文件或程序集'WebGrease,版本 = 1.5.1.25624)是因为 System.Web.Optimization 库引用了未订阅的 webgrease dll 版本。

回答by Alex Albu

Oldie but goldie...

老歌但金...

I was working on a MVC 5 project in Visual Studio 2013, and I had the same problem. We were using Visual SVN for the versioning and I was the only one in my team that had this problem.

我正在 Visual Studio 2013 中处理 MVC 5 项目,我遇到了同样的问题。我们使用 Visual SVN 进行版本控制,我是团队中唯一遇到此问题的人。

The only thing that worked for me was this:

唯一对我有用的是:

  1. Uninstall Microsoft.AspNet.Web.Optimization
  2. In Nuget command prompt run: install-package Microsoft.AspNet.Web.Optimization -Version 1.0.0
  1. 卸载 Microsoft.AspNet.Web.Optimization
  2. 在 Nuget 命令提示符下运行: install-package Microsoft.AspNet.Web.Optimization -Version 1.0.0

This will install an older version of Web.Optimization. The project was initially referencing version 1.1.0. The problem is not with WebGrease, but with the System.Web.Optimization.dll that is referencing an old, inexisting version of WebGrease.

这将安装旧版本的 Web.Optimization。该项目最初引用版本 1.1.0。问题不在于 WebGrease,而在于 System.Web.Optimization.dll 引用了旧的、不存在的 WebGrease 版本。

When I ran the project, it worked, but I wanted to use the same package version as the rest of team. So, I tried to update but... the error returned...

当我运行该项目时,它起作用了,但我想使用与团队其他成员相同的包版本。所以,我尝试更新,但是......错误返回......

After this, I deleted the project (again) and took it back from the SVN. To my surprize, it started to work... The thing is that I had deleted the project completely and took it from the SVN several times before this. I even took the dlls from a colleague because I thought that maybe I'm getting corrupted files from NuGet, but to no avail.

在此之后,我(再次)删除了该项目并将其从 SVN 中取回。令我惊讶的是,它开始工作了......问题是我已经完全删除了该项目并在此之前多次从SVN中取出它。我什至从一位同事那里获取了 dll,因为我认为我可能从 NuGet 获取了损坏的文件,但无济于事。

I hope this will help someone, someday.. with Visual Studio 2016 and MVC 8 :)

我希望这会帮助某人,有一天......使用 Visual Studio 2016 和 MVC 8 :)

回答by André Pena

This error is because Microsoft.AspNet.Web.Optimization 1.1.3internally references WebGrease 1.5.1.25624even though the Nuget package, itself, has a dependency on WebGrease 1.5.2.14234. Someone clearly messed up while creating the Nuget package.

这个错误是因为Microsoft.AspNet.Web.Optimization 1.1.3内部参考WebGrease 1.5.1.25624即使NuGet包,本身具有的依赖关系WebGrease 1.5.2.14234。显然有人在创建 Nuget 包时搞砸了。

To solve this, add this assembly binding in your Web.Config.

要解决此问题,请在您的Web.Config.

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

回答by jparthj

For me, none of above scenarios worked.

对我来说,上述情况都没有奏效。

After trying for two days, finally i found the solution.

尝试了两天,终于找到了解决办法。

What i did was, i uninstalled Microsoft.AspNet.Web.Optimizationand WebGreaseboth.

我所做的是,我卸载了Microsoft.AspNet.Web.OptimizationWebGrease

I knew that this optimization assembly internally refers to WebGrease 1.5.1. So i chose a version of optimization which does not refer to WebGrease 1.5.1.

我知道这个优化程序集内部是指 WebGrease 1.5.1。所以我选择了一个不涉及 WebGrease 1.5.1 的优化版本。

I ran following commands in order to make everything work.

我运行以下命令以使一切正常。

Uninstall-Package Microsoft.AspNet.Web.Optimization
Uninstall-Package WebGrease
Install-Package Microsoft.AspNet.Web.Optimization -Version 1.1.0

I hope i will be able to be a good help for someone for whom above solution does not work.

我希望我能够为上述解决方案不起作用的人提供很好的帮助。

Cheers!

干杯!

回答by tno2007

I had this issue during a deployment.

我在部署期间遇到了这个问题。

I copied WebGrease.dll to production, but I forgot to update the Web.Config file as well.

我将 WebGrease.dll 复制到生产环境,但我也忘记更新 Web.Config 文件。

<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.14234" newVersion="1.5.2.14234" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

回答by kenjamin

When you redirect webgrease make sure you don't have an appliesToattribute on your assemblyBindingelement. For me I removed it completely.

当您重定向webgrease确保你没有一个appliesTo您的属性assemblyBinding元素。对我来说,我完全删除了它。

enter image description here

在此处输入图片说明

回答by jbooker

The issue I observed matched closely what Jidheesh Rajan mentioned. However, updating the package from Nuget package manager (without specifying version) did not fix the issue. Here is what I had to do to fix the issue. (Essentially, I explicity updated WebGrease to version 1.6)

我观察到的问题与 Jidheesh Rajan 提到的非常吻合。但是,从 Nuget 包管理器更新包(不指定版本)并没有解决问题。这是我必须做的来解决这个问题。(本质上,我明确地将 WebGrease 更新到了 1.6 版)

From within the package manager console in Visual Studio, explicitly update WebGrease to version 1.6 . here is the command.

在 Visual Studio 的包管理器控制台中,将 WebGrease 显式更新到版本 1.6 。这是命令。

Install-Package WebGrease -Version 1.6

Output should be

输出应该是

Removing 'WebGrease 1.5.2' from {project name}

Adding 'WebGrease 1.6.0' to {project name} Successfully added 'WebGrease 1.6.0' to {project name} Uninstalling 'WebGrease 1.5.2'. Successfully uninstalled 'WebGrease 1.5.2'.

将“WebGrease 1.6.0”添加到{project name} 成功地将“WebGrease 1.6.0”添加到{project name} 卸载“WebGrease 1.5.2”。成功卸载“WebGrease 1.5.2”。

This ended up updating web.config with

这最终更新了 web.config

    assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /
    bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" 

This also resulted in the removal of a line from the csproj file.

这也导致从 csproj 文件中删除了一行。

<WebGreaseLibPath>..\packages\WebGrease.1.5.2\lib</WebGreaseLibPath>

and an update in this node in the csproj file

以及 csproj 文件中此节点的更新

<Reference Include="WebGrease, Version=1.5.2.14234, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <HintPath>..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>
  <SpecificVersion>False</SpecificVersion>
  <Private>True</Private>
</Reference>

to this

对此

<Reference Include="WebGrease, Version=1.6.5135.21930, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <HintPath>..\packages\WebGrease.1.6.0\lib\WebGrease.dll</HintPath>
  <SpecificVersion>False</SpecificVersion>
  <Private>True</Private>
</Reference>

Keep in mind that I didnt have an issue on my local machine, just on a stage web server. I wanted to try my best to resolve the issue without updating the package, but this was the only fix I had.

请记住,我在本地机器上没有问题,只是在舞台 Web 服务器上。我想尽最大努力在不更新软件包的情况下解决问题,但这是我唯一的修复方法。