{"id":891,"date":"2018-06-01T14:12:46","date_gmt":"2018-06-01T14:12:46","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=891"},"modified":"2026-04-02T09:53:14","modified_gmt":"2026-04-02T09:53:14","slug":"bundling-and-minification-in-asp-net-web-forms-application","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/bundling-and-minification-in-asp-net-web-forms-application\/","title":{"rendered":"Bundling and Minification in ASP.NET Web Forms Application"},"content":{"rendered":"<p>ASP.NET has in-built for bundling of multiple resources such as js or css files into one file and then minifiy the files to reduce the number of calls made to the server and total data size downloaded from the server thus reducing the total download time and enhanding application&#8217;s performance.\u00a0 In order to use this in web forms application which target version .NET 4.5 and higher, following steps are required<\/p>\n<p>1. Go to NuGet Package Manager and install\u00a0Microsoft.AspNet.Web.Optimization and Microsoft.AspNet.Web.Optimization.WebForms\u00a0packages<\/p>\n<p>2. Check web.config to make sure &#8220;webopt&#8221; tag prefix is added (once you install Microsoft.AspNet.Web.Optimization.WebForms)<\/p>\n<pre class=\"lang:default decode:true \">&lt;pages&gt;\r\n  &lt;namespaces&gt;\r\n    &lt;add namespace=\"System.Web.Optimization\" \/&gt;\r\n  &lt;\/namespaces&gt;\r\n  &lt;controls&gt;\r\n    &lt;add assembly=\"Microsoft.AspNet.Web.Optimization.WebForms\" namespace=\"Microsoft.AspNet.Web.Optimization.WebForms\" tagPrefix=\"webopt\" \/&gt;\r\n  &lt;\/controls&gt;\r\n&lt;\/pages&gt;<\/pre>\n<p>3.\u00a0Add a class file as following and define the resources you want to bundle<\/p>\n<pre class=\"lang:default decode:true \">public class BundleConfig\r\n{\r\n    public static void RegisterBundles(BundleCollection bundles)\r\n    {\r\n        Bundle cs = new Bundle(\"~\/bundles\/cssv1\", new CssMinify());\r\n        cs.Include(\"~\/Resources\/css\/bootstrap.min.css\",\r\n            \"~\/Resources\/css\/app.css\");\r\n        bundles.Add(cs);\r\n\r\n        bundles.Add(new ScriptBundle(\"~\/bundles\/jsv1\").Include(\r\n            \"~\/Resources\/js\/jquery.min.js\", \r\n            \"~\/Resources\/js\/bootstrap.min.js\"));\r\n\r\n        BundleTable.EnableOptimizations = true;\r\n    }\r\n}<\/pre>\n<p>4. Go to Global.asax file and register the bundles<\/p>\n<pre class=\"lang:default mark:6 decode:true \">protected void Application_Start(object sender, EventArgs e)\r\n{\r\n    try\r\n    {\r\n        RegisterRoutes(RouteTable.Routes);\r\n        BundleConfig.RegisterBundles(BundleTable.Bundles);\r\n    }\r\n    catch (Exception ex)\r\n    {\r\n        \/\/ Log Exception here\r\n    }\r\n}\r\n<\/pre>\n<p>5. In order to use these bundles simply use the statements as following<\/p>\n<p>CSS bundle<\/p>\n<pre class=\"lang:default decode:true\">&lt;asp:PlaceHolder ID=\"PlaceHolder1\" runat=\"server\"&gt;\r\n    &lt;webopt:BundleReference ID=\"Br1\" runat=\"server\" Path=\"~\/bundles\/cssv1\" \/&gt;\r\n&lt;\/asp:PlaceHolder&gt;<\/pre>\n<p>JS bundle<\/p>\n<pre class=\"lang:default decode:true \">&lt;asp:PlaceHolder ID=\"PlaceHolder1\" runat=\"server\"&gt;\r\n    &lt;%: Scripts.Render(\"~\/bundles\/jsv1\") %&gt;\r\n&lt;\/asp:PlaceHolder&gt;\r\n<\/pre>\n<p>Read more about this @<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/mvc\/overview\/performance\/bundling-and-minification\" target=\"_blank\">Bundling and Minification<\/a><\/p>\n<p><a href=\"https:\/\/blogs.msdn.microsoft.com\/rickandy\/2012\/08\/14\/adding-bundling-and-minification-to-web-forms\/\" target=\"_blank\">Adding Bundling and Minification to Web Forms<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET has in-built for bundling of multiple resources such as js or css files into one file and then minifiy the files to reduce the number of calls made to the server and total data size downloaded from the server thus reducing the total download time and enhanding application&#8217;s performance.\u00a0 In order to use this in web forms application which target version .NET 4.5 and higher, following steps are required 1. Go to NuGet Package Manager and install\u00a0Microsoft.AspNet.Web.Optimization and Microsoft.AspNet.Web.Optimization.WebForms\u00a0packages 2. Check web.config to make sure &#8220;webopt&#8221; tag prefix is added (once you install Microsoft.AspNet.Web.Optimization.WebForms) &lt;pages&gt; &lt;namespaces&gt; &lt;add namespace=&#8221;System.Web.Optimization&#8221; \/&gt; &lt;\/namespaces&gt; &lt;controls&gt; &lt;add assembly=&#8221;Microsoft.AspNet.Web.Optimization.WebForms&#8221; namespace=&#8221;Microsoft.AspNet.Web.Optimization.WebForms&#8221; tagPrefix=&#8221;webopt&#8221; \/&gt; &lt;\/controls&gt; &lt;\/pages&gt; 3.\u00a0Add a class file as following and define the resources you want to bundle public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { Bundle cs = new Bundle(&#8220;~\/bundles\/cssv1&#8221;, new CssMinify()); cs.Include(&#8220;~\/Resources\/css\/bootstrap.min.css&#8221;, &#8220;~\/Resources\/css\/app.css&#8221;); bundles.Add(cs); bundles.Add(new ScriptBundle(&#8220;~\/bundles\/jsv1&#8221;).Include( &#8220;~\/Resources\/js\/jquery.min.js&#8221;, &#8220;~\/Resources\/js\/bootstrap.min.js&#8221;)); BundleTable.EnableOptimizations = true;[&#8230;]<\/p>\n","protected":false},"author":5,"featured_media":1539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,14],"tags":[],"class_list":["post-891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-how-to"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=891"}],"version-history":[{"count":4,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/891\/revisions"}],"predecessor-version":[{"id":902,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/891\/revisions\/902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/1539"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}