Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebGrease error on Styles.Render call in _Layout.cshtml in Mvc4

I'm trying to add bundling to my Mvc4 web project. The steps I took:

  1. Added a reference to my project to System.Web.Optimization
  2. Added @Styles.Render("~/Content/css") within <head> of my _Layout.cshtml
  3. Added BundleConfig.RegisterBundles(BundleTable.Bundles); to Global.asax.cs
  4. Added BundleConfig.cs to my App_Start folder with bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); within RegisterBundles(...) method
  5. Added a simple site.css within my Content folder.

The error that I get on the line from step #2 above is Could not load file or assembly 'WebGrease, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

enter image description here

Am I missing a DLL reference or something? Note that I started originally from a blank Mvc4 project.

like image 414
Ian Davis Avatar asked Dec 27 '12 15:12

Ian Davis


2 Answers

Over the last day or so I was having this issue too but I figured out a solution. Basically you need to just update "webgrease".

Here's what I did to update webgrease:

  1. right click your project solution in your solution explorer.
  2. Click "Manage NuGet Packages for Solution"
  3. Go to the updates section on the left
  4. Search "WebGrease"
  5. Then update "WebGrease"

This worked for me when I built my solution.

like image 114
rishtal Avatar answered Nov 05 '22 11:11

rishtal


I had the same problem after having added ASP.NET MVC 5 to my old ASP.NET web application, running WebForms. I want to add new features and build them on the MVC platform, so it is important that I get MVC and the traditional web application to run side by side. I'm writing this answer here because all the solutions I have found on stackoverflow and other places, including the solution selected to the topic you're reading now, didn't help me. Maybe my findings will help someone out there...

When I added MVC 5 to the web application project, it installed a bunch of references, for example System.Web.Mvc, System.Web.Razor, etc. The NuGet package called Microsoft.AspNet.Web.Optimization was also installed, which has a reference to WebGrease; the reference you and I had problems with.

The version of the Optimization dll that was installed in my web project, is Microsoft.AspNet.Web.Optimization 1.1.3, which is the latest version as of right now. WebGrease version 1.5.2 comes with it.

The funny thing is that the error message I got when executing the @Styles.Render("~/Content/css") statement in _Layout.cs, referred to WebGrease version 1.5.1. I have no idea where it got the 1.5.1 version from, because 1.5.2 was installed in my web project. I tried uninstalling WebGrease and reinstalling, tried upgrading WebGrease to the latest version (1.6.5135). I made certain that none of these dlls were installed in the GAC (which they weren't). I deleted all temporary files in "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\", but all to no avail.

I used the program called Fuslogvw.exe to log all binding information when I ran my web application, in the hope that I would spot a logical reason for why my application insisted on trying to load an old, non-existing version of WebGrease when running my application. Interestingly, the log's output showed me what the application was doing, but unfortunately didn't tell me why. The log told me that despite me having installed Microsoft.AspNet.Web.Optimization version 1.1.3 with WebGrease version 1.5.2, it tried to load Microsoft.AspNet.Web.Optimization version 1.1.0.0, which also comes with an older version of WebGrease. Why it did this? I have no idea, as I've never added Optimization to any project on my PC before.

My solution was to remove both Microsoft.AspNet.Web.Optimization and WebGrease (in that order), and then run the following command in Package Manager Console to install Microsoft.AspNet.Web.Optimization version 1.1.0:

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

This resulted in WebGrease version 1.3.0 being installed, which didn't cause any problems when loading my MVC razor view which uses the _Layout.cshtml page.

For those of you who are interested: below you can see the output from the log produced by Fuslogvw.exe, where it clearly states that it tried to load the non-existing 1.1.0 version of Optimization:

*** Assembly Binder Log Entry  (26.11.2018 @ 13:19:59) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  c:\windows\system32\inetsrv\w3wp.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/Projects/MyProject/Web/
LOG: Initial PrivatePath = C:\Projects\MyProject\Web\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\myproject\1db399c5
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\myproject\1db399c5
LOG: AppName = 3f7f1cb1
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\MyProject\Web\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/ipmvcang/1db399c5/3f7f1cb1/WebGrease.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/ipmvcang/1db399c5/3f7f1cb1/WebGrease/WebGrease.DLL.
LOG: Attempting download of new URL file:///C:/Projects/MyProject/Web/bin/WebGrease.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Projcets\MyProject\Web\bin\WebGrease.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: WebGrease, Version=1.6.5135.21930, Culture=neutral, PublicKeyToken=31bf3856ad364e35
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
like image 1
Ittit Avatar answered Nov 05 '22 11:11

Ittit