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: 0x8013
This error may occur on a production server (IIS 7 in my case), while everything works fine in your development environment (VisualStudio 2012), even if you use IIS and not the bulit-in ASP.NET Development server.
What happens is, when you upgrade the NuGet package in VisualStudio 2012, VS adds the following block to the web.config file in your project: (inside the <configuration/> ælement). This is an example from my own file:
<runtime>
<assemblyBindingxmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentityname="System.Web.Optimization"publicKeyToken="31bf3856ad364e35"culture="neutral"/>
<bindingRedirectoldVersion="0.0.0.0-1.1.0.0"newVersion="1.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentityname="WebGrease"publicKeyToken="31bf3856ad364e35"culture="neutral"/>
<bindingRedirectoldVersion="0.0.0.0-1.1.0.0"newVersion="1.5.1.25624"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
As long as everything works on the development machine, we don’t think about the actual version of the WebGrease.dll being used by the runtime. You might have several of them on your development machine. I have tried to manually remove the reference to the WebGrease.dll, then add it again to the project, but the actual version No in the above-mentioned block in web.config remained unchanged.
That is why for some time I didn’t pay attention to the fact, that the actual version of the WebGrease.dll in my project BIN folder was 1.6.x.x,. This version was also copied to the production environment.
I have read A LOT of tweaks about this issue, NONE of them worked for me. For instance, removing xmlns="urn:schemas-microsoft-com:asm.v1" in the top line.
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> changed nothing, but I am glad, if it worked for he others.
Some guys out there @ Stackoverflow shared an impression, the whole block is simply being ignored by the runtime, and I had such an impression, too.
There are also tips, advising to remove the complete block <assemblyBinding></assemblyBinding> from the web.config file, but this doesn’t work, either.
So, finally, I have figured out, the error message/exception must be accurate – the runtime simply can’t find the version specified in the web.config.
Thus, I suggest you check the actual version of the WebGrease.dll in the BIN folder of your project and put this version No into your bindingRedirect directive as the new version:
<bindingRedirectoldVersion="0.0.0.0-1.6.5135.21930"newVersion="1.6.5135.21930"/>
That should work, as it did for me. Happy coding!