Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off bundling/minification while debugging in WebForms

I want to be able to minify/bundle my javascript files in a production environment while having them unminified/unbundled when debugging locally; however, the default WebForms site in VS2012 does not appear to allow for it.

Steps to reproduce my issue:

  1. Create a new "ASP.NET Web Forms Application" (c# in my case)
  2. Start without debugging and view resources in browser
  3. Notice unminified jquery/modernizr files but bundled/minified MsAjaxJS and WebFormsJs

web.config, by default has:

<compilation debug="true" targetFramework="4.5">

And I've even tried modifying the Global.asax by explicitly telling BundleTable not to optimize:

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    BundleTable.EnableOptimizations = false;
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AuthConfig.RegisterOpenAuth();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

Yet, I still get undebuggable javascript files:

Chrome Dev tools (showing minified/bundled files)

This site (which I realize is for MVC) tells me that either debug="true" in the web.config or BundleTable.EnableOptimizations = false; in Global.asax should turn off the functionality. And this site, (which is for WebForms and is linked from the first), doesn't mention turning it off for debugging.

How might I (cleanly) tell my project to only minify/bundle when debug="false"?

(I'm using ASP.NET 4.5)

EDIT:
This question and this question are similar, but they both only offer a solution using Scripts.Render(). Is there a way to accomplish this with the templated method of using the ScriptManager?

like image 563
snumpy Avatar asked Apr 03 '14 20:04

snumpy


People also ask

How do I turn off my bundling?

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled.

How do you override the bundling setting of web config?

Also, you can enable or disable bundling and minification by setting the EnableOptimizations property of the BundleTable class. This overrides the web. config settings. In the following example, EnableOptimizations is set to true to enable bundling and minification.

Which of these is the correct way to configure bundling and minification?

Click on wwwroot folder, select the css file you want minified, then right click and choose bundler & minifier. Then from popup minify file. It will be the same file name with the minified version. Also generate bundleconfig.


2 Answers

Add the following code to your Global.asax.cs file in the Application_Start method. This works perfectly for me.

#if DEBUG
       foreach (var bundle in BundleTable.Bundles)
       {
           bundle.Transforms.Clear();
       }
#endif
like image 196
Justin Russo Avatar answered Nov 14 '22 23:11

Justin Russo


I had a similar issue before. I solved my problem by putting this code

BundleTable.EnableOptimizations = false;

after

BundleConfig.RegisterBundles(BundleTable.Bundles);
like image 36
Dmitry Kovaliov Avatar answered Nov 14 '22 21:11

Dmitry Kovaliov