Introduction
- http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
- http://msdn.microsoft.com/en-us/magazine/dn451436.aspx
Bundling
Minification
- function sum(a,b)
- {
- return (a + b); //will return the sum of numbers
- }
- function sum(a,b){return (a+b);}
- Having many common JavaScript/CSS files used throughout the application
- Too much JavaScript/CSS files in use in a page
Bundles should be created per usability in the pages where we actually need them. For example, an application common CSS file can be added to a single bundle, all common JavaScript files can be added to a single bundle. We even can create page-specific bundles.
Where we can Apply this: With ASP.NET we can apply it on MVC as well as ASP.NET forms or anywhere we are handling JavaScript/CSS.
Procedure to apply bundling: The following is the procedure to apply bundling.
Step 1
Add the Nuget package “Microsoft ASP.NET Web Optimization Framework”.
We can see that adding this Nuget package adds a few lines in our web.config files too as in the following:

- <runtime>
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <dependentAssembly>
- <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
- <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
- </dependentAssembly>
- </assemblyBinding>
- </runtime>
Step 2
Add BundleConfig.cs to the App_Start folder. If the folder does not exist then please add it first.
Open BundleConfig.cs and add “System.Web.Optimization namespace” in the using section. The code of BundleConfig.cs will look as in the following:

- using System.Web.Optimization;
- namespace medAppz2.App_Start
- {
- public class BundleConfig
- {
- public static void RegisterBundle(BundleCollection bundle)
- {
- //bundle all common js files, required in every page
- bundle.Add(new ScriptBundle("~/bundles/MyAppStartupJs")
- .Include("~/include/js/General.js",
- "~/include/js/Validation.js",
- "~/include/js/NormalAbnormalCheck.js"));
- //wrapup all css in a bundle
- bundle.Add(new StyleBundle("~/bundles/MyAppStartupCss")
- .Include("~/include/style/main.css",
- "~/include/Style/Container.css",
- "~/include/Style/BasicElement.css"));
- BundleTable.EnableOptimizations = true;
- }
- }
- }
- BundleTable.EnableOptimizations = true;.
Step 3
Now to register the bundle in the Application_Start event of the Global.asax.cs file as in the following:
- protected void Application_Start(Object sender, EventArgs e)
- {
- BundleConfig.RegisterBundle(BundleTable.Bundles);
- }
Step 4
Now all the settings are done, we just need to add bundle references in aspx files.
Step 5
Add the following line to your page. It will allow the use of Styles.Render and Scripts.Render methods on the aspx page.
- <%@ Import Namespace= "System.Web.Optimization" %>
Note: With MVC we don't need to use the line above.
Step 6
To render these files you just need to write the following lines in your head tag. Of course, you can also add JavaScript bundles after the body tag and other places for better performance:
- <% Styles.Render("~/bundles//MyAppStartupCss")%>
- <% Scripts.Render("~/bundles//MyAppStartupJs")%>
- <link href="/BundlingAndminificationDemo/bundles/MyAppStartupCss" rel="stylesheet"/>
- <script src="/BundlingAndminificationDemo/bundles/MyAppStartupJs"></script>

Reza RahgozarPosted Jun 25, 2024, 10:26 AM
Thank you for your article about applying bundling and minification in .NET. The process works fine in my local environment, but I'm encountering issues when deploying it to the host. Could you please provide guidance on how to resolve this?
manish kedarPosted Jul 13, 2020, 4:45 AM
Can we render this styles and scripts on master page in asp.net. Please share how to do this.
manish kedarPosted Jul 13, 2020, 4:44 AM
Can we render this styles and scripts on master page in asp.net
Praveen KumarPosted Jun 8, 2018, 6:35 AM
Can we render all the styles inline instead of bundling all included file into single file in ASP.NET Web Form?
sai jamesPosted Sep 8, 2017, 7:06 AM
Could you please share some scenario based questions and answers on MVC and Azure, if you have.
sai jamesPosted Sep 8, 2017, 7:05 AM
Excellent article Vinod, Thanks for sharing with us.
Gowtham RajamanickamPosted May 28, 2015, 10:45 AM
this is great..
Vinod PandeyPosted Sep 25, 2014, 5:48 AM
Thanks
Saineshwar BageriPosted Sep 21, 2014, 12:01 AM
Nice article sir