andrewm
andrewm

Reputation: 2642

Bundling not working in MVC5 when I turn on release mode

I have the following bundle configured in BundleConfig.cs:

bundles.Add(new StyleBundle("~/bundles/css").Include(
                      "~/assets/bootstrap/css/bootstrap.css",
                      "~/assets/css/global/all.css"));

and I reference it using the following:

@Styles.Render("~/bundles/css")

When I'm in debug mode (web.config compilation debug="true") it works as expected in that it renders both css files as normal ie:

<link href="/assets/bootstrap/css/bootstrap.css" rel="stylesheet"/>
<link href="/assets/css/global/all.css" rel="stylesheet"/>

However when I set debug="false" the above behaviour still occurs in that it does recognise the files, however it's just rendering them as normal.

To confirm bundling can definitely work I've enabled optimizations in BundleConfig ie BundleTable.EnableOptimizations = true;

Whenever I do the above, it bundles the css and appears as expected ie:

<link href="/bundles/css?v=WBKHkZAJly7jUzHrVDT8SwfaQE-CA9dbOUQUlLKadNE1" rel="stylesheet"/>

EDIT:

A few people have mentioned that adding the following code to my BundleConfig.cs file will achieve what I am after:

#if DEBUG
            BundleTable.EnableOptimizations = false;
#else
            BundleTable.EnableOptimizations = true;
#endif

I understand and appreciate this response, however according to the documentation, the default behaviour of MVC bundling is to bundle in release mode but not in debug mode. I don't see why I should need to add extra code to make it do this when it should be doing it already.

EDIT 2

I've a confession to make. It turns out I had the web.config from the Views folder opened and not the main web.config. I changed the setting in the main web.config and this works just fine for me. I blame ReSharper

Upvotes: 33

Views: 32878

Answers (9)

Tjaart van der Walt
Tjaart van der Walt

Reputation: 5179

This is the default behavior.

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web.config file.

<system.web>
    <compilation debug="true" />
    <!-- Lines removed for clarity. -->
</system.web>

To enable bundling and minification, set the debug value to "false". You can override the Web.config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.config file.

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                 "~/Scripts/jquery-{version}.js"));

    // Code removed for clarity.
    BundleTable.EnableOptimizations = true;
}

http://www.asp.net/mvc/overview/performance/bundling-and-minification

enter image description here

Upvotes: 32

RaZzLe
RaZzLe

Reputation: 2128

In addition to all other replies, if you still cannot bundle your CSS and JS files, be sure that you are addressing the correct paths and/or folder/file names.

In my case, some of the files I'm addressing are actually missing, and some has typo while addressing. After you correct these kind of things bundle should work properly.

Lastly, if you have references via relative paths on your CSS or JS files (e.g. url(../../css/img/loading.gif)), you might want to turn them into absolute paths (e.g. url(/Content/css/img/loading.gif)) otherwise they won't be worked or rendered eventhough files are successfully bundled. Relative paths in bundle show differences from browsers' point of view.

Upvotes: 0

Tongi
Tongi

Reputation: 155

Make sure that you do not have any minified file in you BundleConfig class, e.g bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/js/popper.min.js"));

Instead use bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/js/popper.js"));

Upvotes: 0

Gopal Nyaupane
Gopal Nyaupane

Reputation: 1

<compilation debug="true" targetFramework="4.5.2" />

solve my problem. debug=true was missing in my web config while publishing.

Upvotes: -3

Rodrigo Peplau
Rodrigo Peplau

Reputation: 180

After fighting against this issue for several hours, I recommend that you try this as well:

  1. Add this at the very beginning of the view you are using your bundle:

    @{ BundleTable.EnableOptimizations = true; }

  2. Reset IIS

  3. Reload your page and check if minify works

In my case somewhere in my solution was changing "BundleTable.EnableOptimizations" from true to false. When I force that to TRUE right before the bundle is used, I got this working.

After I noticed it works, I moved that into a static method at my BundleConfig class:

public static void EnableOptimizations()
{
    #if DEBUG
        BundleTable.EnableOptimizations = false;
    #else
        BundleTable.EnableOptimizations = true;
    #endif
}

So I can call it from the view and have it minify disabled for developers

@{
    BundleConfig.EnableOptimizations();
}

Upvotes: 1

Antonio Rodr&#237;guez
Antonio Rodr&#237;guez

Reputation: 1126

I had this problem when I changed the css folder. My problem was that I changed the BundleConfig file for css files:

BundleConfig.cs:

bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/css/bootstrap.css",
   ....

Views included:

@Styles.Render("~/Content/css")

For some reason, this didn't worked. 'bundles' seems to be a keyword or something (not sure). You must left it this way:

bundles.Add(new StyleBundle("~/bundles/css").Include(
                      "~/Content/css/bootstrap.css", ...

Views:

...
@Styles.Render("~/bundles/css")
...

I have no need to modify 'BundleTable.EnableOptimizations' nor web.config nor anything else.

Hope this helps someone.

Upvotes: 2

Sal Alturaigi
Sal Alturaigi

Reputation: 490

Another possible issue is that inside of your Global.asax.cs file in the Application_Start() method, you're missing the call to your BundleConfig.

For example, assuming your method is using the default name RegisterBundles(BundleCollection bundles) then inside of your Global.asax.cs file, you'll want to add BundleConfig.RegisterBundles(BundleTable.Bundles); in the Application_Start() method.

So your Global.asax.cs file might look something like this:

using System;
using System.Collection.Generic;
using System.Linq;
using System.Web;
using System.Http;
using System.Mvc;
using System.Routing;
using System.Optimization;

namespace MyProject
{
    public class MvcApplication : System.Web.HttpApplication
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        // ** This line right below might be what you are missing **
        BundleConfig.RegisterBundles(BundleTable.Bundles);

    }
}

Upvotes: 5

davrob01
davrob01

Reputation: 373

The default Release Web.config transform removes the debug attribute like so:

<compilation xdt:Transform="RemoveAttributes(debug)" />

However, this will not cause the expected bundling behavior to occur. Instead, you must create a transform that literally sets the debug attribute to "false", like so:

<compilation debug="false" xdt:Transform="SetAttributes" />

Upvotes: 6

krillgar
krillgar

Reputation: 12815

The way that I get around this is to force it in the BundleConfig to do exactly what I want it to do. I don't think MVC4 had the same options with the config file (or I just never got them to work).

So this is what I have at the end of my RegisterBundles method:

#if DEBUG
BundleTable.EnableOptimizations = false;
#else
BundleTable.EnableOptimizations = true;
#endif

This way it's always there, plain to see. However, you do have to remember to put that in there when you're starting up the project, but that's not a huge deal.

If you're not familiar with these, the #if DEBUG is a preprocessor directives that tells the CLR to do what is in that block when the DEBUG build parameter is present (should only be present in DEBUG mode, though that can be changed from the Project Properties). If that variable is not present (Release mode, or any other mode), then it will do the other block.

Upvotes: 16

Related Questions