72

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

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.

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

And tried with changing 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" />

19 Answers 19

85

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 4 NuGet package, in my case it had something to do with the Microsoft.AspNet.Web.Optimization package, and managed to find a simple solution.

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 4 package, but I ran them anyway and then it all compiled and ran great.

6
  • 1
    I was lucky and it updated to version 1.6 and fixed the error for me. Its just a version issue.
    – Piotr Kula
    Apr 8, 2014 at 21:22
  • For using in a WebForms context there is also the package Microsoft.AspNet.Web.Optimization.WebForms which you should add before/after the lines where Microsoft.AspNet.Web.Optimization is mentioned.
    – Uwe Keim
    Jun 30, 2014 at 19:27
  • And also restart Visual Studio to take fully effect of the above commands.
    – Uwe Keim
    Jun 30, 2014 at 19:27
  • When doing this make sure you have no xmlns attribute on your configuration tag in web.config if you do it generates a invalid xmlns attribute on your assemblyBinding tag.
    – Peter
    Mar 10, 2015 at 11:50
  • 1
    (When copy/paste the nuget commands, ensure the last one in the batch executes. It could be sitting there waiting on the command prompt E.g. Update-Package WebGrease) This answer helped. But I was also missing web.config entries afterwards. See @andrerpena answer too.
    – Valamas
    Jun 19, 2015 at 0:31
64

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

To solve this, add this assembly binding in your 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>
7
  • Problem is simple indeed, so this anwer should easily help Oct 28, 2014 at 6:06
  • 10
    This fixed it for me. I updated all packages on a new project and got version 1.6. Following the above instructions, but updating new version appropriately, it worked: <bindingRedirect oldVersion="0.0.0.0-1.5.1.25624" newVersion="1.6.5135.21930" />
    – Todd
    Jan 17, 2015 at 1:29
  • 2
    Simple and efficient answer. Thanks Nov 10, 2015 at 19:45
  • 1
    And here is the root reason: stackoverflow.com/a/46898880/1145224 why this happens and where to find correct dependentAssembly configuration. Oct 23, 2017 at 21:42
  • 3
    As a "not expert" with web.config, I thought I'd mention that dependentAssembly is nested thusly: <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> Nov 16, 2018 at 21:35
47

Have you tried through NuGet ?

Install-Package WebGrease -Version 1.5.1

or

Install-Package WebGrease -Version 1.5.2
3
  • 10
    Any references I could find in my project pointed to 1.5.2, but I was somehow still getting this error. 'Install-Package WebGrease -Version 1.5.2' worked for me. All it did was add this to my web.config: <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly>
    – Sean
    Mar 6, 2014 at 2:37
  • 4
    I had to add the above to the web.config manually, but that resolved it for me.
    – Timm
    Apr 15, 2014 at 15:12
  • I've tried all of the suggestions on this page but can't get past this error. I created a clean MVC web application (.net framwork) but get an error on @Styles.Render("~/Content/css") in _Layout.cshtml. Not sure what else to do. Using Microsoft.AspNet.Web.Optimization 1.1.3 with WebGrease 1.5.2 or 1.6 still result in "Could not load file or assembly 'WebGrease, Version=1.5.1.25624". Updating bindingRedirect to web.config didn't work either. Can someone please help? Feb 25, 2021 at 22:06
17

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.Optimization and WebGrease both.

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.

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!

5
  • Also had to manually update the web.config entry, but yeha this worked for me, thanks!
    – Jensen
    May 28, 2015 at 13:05
  • your solution worked, this is crazy. I just downloaded VS 2013 CTP and deployed a new MVC 5 project to a new Win Server 2012 R2 and I received the WebGrease error... even after updating the Web.Config your solution was the only one that worked.
    – InContext
    Jun 22, 2015 at 5:45
  • @InContext i ran into same problem and as i said above none of the solution in the thread helped me but this. happy coding!
    – jparthj
    Jun 29, 2015 at 20:20
  • also realised (more importantly) my Web.Config was not in the right place and so the bindingRedirect was not being used... after sorting that this fixed up as well!
    – InContext
    Jun 30, 2015 at 9:25
  • my website was working well on the dev machine whereas it wasn't working on the deployment server. what I did, follow the above step, and redeploy the website and it works for me. thanks.
    – Haseeb
    Oct 7, 2020 at 12:32
8

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)

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

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'.

This ended up updating web.config with

    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.

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

and an update in this node in the csproj file

<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.

5

Tried a lot of things but this worked for me sometimes webgrease assembly issues sometimes Web.Optimization assembly issues. This worked for me with Microsoft.AspNet.Web.Optimization 1.1.3

Update-Package WebGrease -Version 1.6
2
  • This worked for me. Trying to install 1.5.1 threw errors because dependencies would break. May 27, 2016 at 15:26
  • 1
    yes apparently there is some bug in this version. They've fixed it in 1.6
    – Ali Umair
    May 27, 2016 at 15:30
4

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.

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

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.

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.

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

4

When you redirect webgrease make sure you don't have an appliesTo attribute on your assemblyBinding element. For me I removed it completely.

enter image description here

2

Since I wasn't able to uninstall using package manger console, I manually went to packages.config and comment out the older version.

2
  • Maybe you can explain further to make your answer clearer?
    – Ali Bdeir
    Oct 20, 2016 at 17:46
  • open packages.config in Visual studio and go to <package id="WebGrease" version="1.5.2" targetFramework="net461" /> comment out the duplicate one Oct 22, 2016 at 13:54
2

I used "Install-Package Microsoft.AspNet.Web.Optimization" (current version is 1.1.3) for starup web-project, which was not selected in NuGet console by default.

But NuGet has created a new app.config file even though web.config is expected to be updated.

For me it worked to move WebGrease dependentAssembly element from app.config to web.config's <runtime><assemblyBinding>...</assemblyBinding></runtime>.

1

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.

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

1

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

// References: WebGrease, Version=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

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:

// References: WebGrease, Version=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.

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.

1

I had this issue during a deployment.

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

<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>
1

In my case this was not solved by any of the solutions above. The error relating to webgrease was actually a red herring ... it was a problem with an invalid (and completely unrelated) assembly binding redirect. I guess this problem with my web.config file was causing all the assembly binding redirects to fail, and it just so happened this was causing a runtime issue with WebGrease.

Basically, during a merge one of the binding redirects had got corrupted and it had ended up with two assembly redirects within a single <dependentAssembly> tag.

So, if you are getting this issue and you already have the binding redirect set up correctly for WebGrease, it is worth scanning through all your other assembly binding redirects to check that non of them have been corrupted.

0

Here is what I did to get around it,

  1. I saved a copy of the Web.config file of my current (corrupted project) to "Corrupted-Web.Config". and close the project solution.
  2. Created a new application with the same type of my courrpted project. called it ProjTemp.
  3. Save and Run the "PorjTemp", then Save a copy of the Web.Config to "OrigTemp_Web.config"
  4. Close the Web.config files.
  5. On the "ProjTemp" open Tools --> "NuGet Package Manager" --> "Manage NuGet Packages for Solutions" ---> Select to Update all related installed packages. 6 Now Open the newly updated Web.config file. Copy the get the info from the Updated "" Starting from "

<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
             --- Copy this section to replace in the corrupted file.---
       </assemblyBinding>
</runtime>

  1. Save your project. Rebuild and Run. Hope this will help! ....

Here is the corrected one for example, VS2015.


<runtime>
           <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                 <dependentAssembly>
                        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
                        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
                 </dependentAssembly>
                <dependentAssembly>
                        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
              </dependentAssembly>
              <dependentAssembly>
                        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
                        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /
              </dependentAssembly>
              <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /
                </dependentAssembly>
                <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                </dependentAssembly>
                <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                 </dependentAssembly>
                 <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                         <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                  </dependentAssembly>
                  <dependentAssembly>
                         <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
                        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
                   </dependentAssembly>
          </assemblyBinding>
  </runtime>

that's what I have done to fix it, Solution on Github Sami, Oweis 4 Computer

0
0

This problem may also arise if you do not deploy Web.config file to the web server.

3
  • 2
    These type of statements are more suitable as comments, earn enough rep and then comment.
    – Karan Shah
    Feb 10, 2017 at 9:47
  • I am just sharing another scenario where the same problem may arise. Even if it sounds naive, this can happen by mistake during deployment. So the solution is to deploy the Web.config file to the server in such a case. You can test this yourself. Feb 11, 2017 at 11:29
  • 1
    Thank you, this was the case for me May 11, 2020 at 11:20
0

VS 2017 seems to have the problem of not deploying the web.config, even though the properties for the file are set to 'copy always'. In my project, I have to swap out different web.config files for various deployment scenarios. And the 'webgrease' error will appear if it did not deploy the file.

Just verify that the web.config file gets deployed.

0

I've faced the excat same issue and managed to fix it by doing the following:

  1. Uninstalling the Microsoft.AspNet.Web.Optimization nuget package.
  2. Uninstalling the Antlr nuget package because its a dependecy for WebGrease package.
  3. Uninstalling the WebGrease nuget package.
  4. Install Microsoft.AspNet.Web.Optimization nuget package again, this action will take care of installing Antlr and WebGrease because they are its dependencies.
0

I ran into this problem after creating a new asp.net MVC project in visual studio 2022. I found it quite bizarre that the unmodified project generated by the template wouldn't run out of the box.

I'm experienced with binding redirects and understood the solutions offered here, but none of them worked. My conclusion was that the binding redirects in my web.config were being ignored, and the fusion log seemed to confirm this.

LOG: No application configuration file found

Turns out the problem was the path where I had the code contained a # (pound sign, hash tag symbol). Luckily I stumbled on another question where this was mentioned as causing problems with assembly binding. Weird. https://stackoverflow.com/a/25577313/3869744

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.