Customizing Bundling and Minification for local and production version – ASP.NET MVC/WEBAPI

Standard

Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time.  Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)

Since minification minify css/JS files [remove extra space, comments, rename variables to shorter name, convert whole code to single line], make it difficult to read and debug so increase security of it, that’s is why in order to debug css/js issues in production, I have used BundlingActive flag in web.config, By default it would be 1, if you want to debug the code, you may change it to zero, but in order to activate bundling and minification, you need to change this flag to one.

You may set the value of variable BundlingActive in webconfig file under appSetting values, for local version of project you may set it to false and after deployment in production you may set the value to true, so that bundling and minification process is carried out on stylesheets and scripts files, built-in feature BundleTable.EnabaleOptimization will help you to achieve successful activation of bundling and minification process on the fly.

public static void RegisterBundles(BundleCollection bundles)
{
    //JScript Bundle
    bundles.Add(new ScriptBundle("~/bundles/SampleJSBundle").Include(
                "~/Scripts/jquery.fixedheadertable.js",
                "~/Scripts/fineuploader.js",
                "~/Scripts/Chosen.js"));
 
    //CSS Bundle
    bundles.Add(new StyleBundle("~/SampleCSS").Include(
                "~/Content/site.css",
                "~/Content/MainMenu/ClassicStyle.css",
                "~/Content/MainMenu/Chosen.css",
                "~/Content/MainMenu/tokeninput.css"));
            
    BundleTable.EnableOptimizations = AppConfig.BundlingActive;
}

Using in Views:
@Styles.Render("~/SampleCSS") 
@Scripts.Render("~/bundles/SampleJSBundle")
Advertisements

2 thoughts on “Customizing Bundling and Minification for local and production version – ASP.NET MVC/WEBAPI

  1. Kennith Hey

    Thank you for sharing superb informations. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found simply the info I already searched everywhere and just couldn’t come across.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s