Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5.1 debug enabled doesn't disable Bundling and minification

Running in debug from VS 2013.2RTM Pro, MVC 5.1 app.

If the compilation mode is set to debug="true" it is supposed to disable Bundling and minification but it does not. When I examine the View source on a page the styles and scripts are bundled.
<script src="/bundles/modernizr?v=K-FFpFNtIXjnmlQamnX3qHX_A5r984M2xbAgcuEm38iv41"></script>

If I set BundleTable.EnableOptimizations = false; in the BundleConfig.cs it does disable Bundling and minification but that is not how it is supposed to work. I shouldn't have to remember to toggle the EnableOptimizations setting!

Things are working properly in VS 2012 MVC 4 apps.

Is this a MVC 5.1 bug? Has anyone else had this problem? Is there a way to get debug to disable the Bundling and minification?

web.config:

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" useFullyQualifiedRedirectUrl="true" maxRequestLength="100000" enableVersionHeader="false" />
    <sessionState cookieName="My_SessionId" />
  <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
  </system.web>

_Layout.cshtml:

In header

@Styles.Render("~/Content/css") @Styles.Render("~/Content/themes/base/css") @Scripts.Render("~/bundles/modernizr")

At end of body

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") @Scripts.Render("~/bundles/jqueryval")

like image 763
Joe Avatar asked May 17 '14 03:05

Joe


People also ask

How do you use bundling and minification in MVC 5?

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.

Can we use bundling and minification with ASP NET web forms like MVC?

To optimize the performance of an application I found that bundling and minification can significantly improve the performance. It can be applied on MVC as well as in ASP.NET web forms.

What are the two types of bundles in MVC 5?

Bundle TypesScriptBundle: ScriptBundle is responsible for JavaScript minification of single or multiple script files. StyleBundle: StyleBundle is responsible for CSS minification of single or multiple style sheet files.

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.


1 Answers

You may have a look at this article http://codemares.blogspot.com.eg/2012/03/disable-minification-with-mvc-4-bundles.html

or you can use this simple implementation

public class NoMinifyTransform : JsMinify
{
    public override void Process(BundleContext context, BundleResponse response)
    {
        context.EnableOptimizations = false;
        var enableInstrumentation = context.EnableInstrumentation;
        context.EnableInstrumentation = true;
        base.Process(context, response);
        context.EnableInstrumentation = enableInstrumentation;
    }
}

and then when defining your script bundles in (App_Start) you can use the base Bundle class like this

            IBundleTransform jsTransformer;
#if DEBUG
            BundleTable.EnableOptimizations = false;
            jsTransformer = new NoMinifyTransform();
#else
            jstransformer = new JsMinify();
#endif
            bundles.Add(new Bundle("~/TestBundle/alljs", jsTransformer)
               .Include("~/Scripts/a.js")
                .Include("~/Scripts/b.js")
                .Include("~/Scripts/c.js"));
like image 161
Rady Avatar answered Nov 15 '22 10:11

Rady