Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundling and not bundling in production and test environments in ASP.NET MVC

I am using the ASP.NET Bundling mechanism:

            BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/Master-js").Include(
                        "~/Scripts/respond.min.js",
                        "~/Scripts/jquery.form.js",
                       "~/Scripts/jquery.MetaData.js",
                        "~/Scripts/jquery.validate.js",
                        "~/Scripts/bootstrap.js",
                        "~/Scripts/jquery.viewport.js",
                        "~/Scripts/jquery.cookie.js"
                     ));

I want this to happen if the build is in release. If the build is in debug, I want the un-minified individual files to load so debugging would be easy.

The only way I have been able to do this is to write the following in my view:

    <%  if(HttpContext.Current.IsDebuggingEnabled)
        {
            Response.Write("<script type='text/javascript' src='../../Scripts/respond.min.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.form.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.MetaData.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.validate.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/bootstrap.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.viewport.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.cookie.js'></script>");
        }
        else
        {
            Scripts.Render("~/Scripts/Master-js");
        }
%> 

As you can see, I am repeating myself here. Is there a better way?

like image 247
Barka Avatar asked Nov 04 '13 18:11

Barka


People also ask

What is bundling and minification in ASP.NET MVC?

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.)

What are the two types of bundles in MVC 5?

You can see ScriptBundle and StyleBundle objects we are using for bundling the js and CSS types of files. ScriptBundle: is responsible for Script files i.e javascript (JS). StyleBundle: is responsible for stylesheet files i.e CSS.

What is the use of bundling in MVC?

To improve the performance of the application, ASP.NET MVC provides inbuilt feature to bundle multiple files into a single, file which in turn improves the page load performance because of fewer HTTP requests.

What are the different ways for bundling and minification in asp net core?

Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.


1 Answers

By default, when using Scripts.Render() on your view and if the application has debugging enabled (debug="true" in the web.config) the files will not be bundled or minified. Also a individual script tag will be rendered for every asset in that bundle.

You can manually enable/disable bundling and minification by using System.Web.Optimization.BundleTable.EnableOptimizations

like image 123
Steven V Avatar answered Sep 29 '22 23:09

Steven V