Webgrease Error on Styles.Render Call in _Layout.Cshtml in MVC4

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

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.

Asp.net gives error when it renders some styles

Make sure your Views/Web.config has

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

And install the latest Microsoft.AspNet.Web.Optimization through NuGet.

ASP.NET MVC Bundle not rendering script files on staging server. It works on development server

Based on your comments, we need to go over how the Bundling mechanism works in MVC.

Edit: Based on the comment below by VSDev, you need to ensure WebGrease is installed into your project. NuGet would be the easiest was to install this package.

When you set a bundle configuration (Example not from above to illustrate)

bundles.Add(new ScriptBundle("~/bundles/mainJs")
.Include("~/Scripts/mainSite.js")
.Include("~/Scripts/helperStuff.js"));

You then, in your views, call something like @Scripts.Render("~/bundles/mainJs"). When your web.config is set into a debug compilation OR you explicitly turn off bundling using the following line in your BundleConfig.cs file

BundleTable.EnableOptimizations = false;

Then, in your view, you will see the following rendered out

<script src="/Scripts/mainSite.js" type="text/javascript"></script>
<script src="/Scripts/helperStuff.js" type="text/javascript"></script>

These are the individual items that made up our bundle, uncompressed and listed individually. The reason these list out individually in debug mode is so that you can debug your scripts and see them as you wrote them (actual variable names, etc).

Now, when we are not in a debug compilation and have not turned off the EnableOptimizations feature, MVC will combine those files in our bundles, compress (minify) them and output only a single script tag.

<script src="/bundles/mainJs?v=someBigLongNumber" type="text/javascript"></script>

Notice that the source is the same as the name of the bundle from the bundle configurations. Also, the number after the ?v= will change anytime you change a file in that bundle. This is to help prevent caching of old js and css files by the client browsers.

Your scripts are still there and being outputted, but they are being compressed and combined into a single file called /bundles/mainJs. This feature is present to

A) compress the files and reduce information being transmitted and,

B) reduce the number of calls to a website to retrieve the necessary content to render the page.

Nothing is missing, it sounds like everything is working as intended. In a production site, the minification makes these files almost impossible to read, thus why the minification does not take affect while debugging.

As to why the jQuery UI is still being a single JS file, ensure someone didn't hard code that into your layout view. As for the JS errors, it could be errors that are present on your development box or perhaps something did not compress correctly (however, in all of my MVC development, I have not seen a JS error because of bad minification).

Visual Studio MVC 5 shows errors but compiles and runs okay

The most likely cause of this issue is that the web.config in your Views folder(s) is/are broken. Upgrading the ASP.NET MVC 5 requires quite a few configuration changes that I am not certain the NuGet package manager does (or does well, I think it takes care of a few of these). Along with the MVC 5 .dll, many of the other related assemblies also need to be updated and the related references updated as well.

Take a look at the following tutorial and ensure you have completed EACH of the required steps. Then do a clean, rebuild and see if your issues are resolved.

I have found that it is sometimes better to have VS create a new MVC project, then look at and compare the web.configs (both application root as well as the views folder web.config) that it generates to your project's configuration files to ensure that you are not including namespaces that don't belong and all version numbers are correct.



Related Topics



Leave a reply



Submit