How to Bundling and Minification in ASP.NET MVC
By FoxLearn 2/18/2024 1:32:44 AM 311
Bundling and minification are very useful to improve request load time. It improves load time by reducing the number of requests to the server and reducing the size of requested resources.
Bundling is a new feature that allows you to combine multiple files into a single file and help you improve your page load performance for the first time.
using System.Web; using System.Web.Optimization; namespace Invoice { public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js", "~/Scripts/jquery.unobtrusive-ajax.min.js")); bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.validate*")); bundles.Add(new StyleBundle("~/login/css").Include( "~/assets/css/bootstrap.min.css", "~/assets/css/font-awesome.min.css", "~/assets/css/beyond.min.css", "~/assets/css/animate.min.css")); bundles.Add(new ScriptBundle("~/login/js").Include( "~/assets/js/skins.min.js", "~/assets/js/jquery.min.js", "~/assets/js/bootstrap.min.js", "~/assets/js/slimscroll/jquery.slimscroll.min.js", "~/assets/js/beyond.js")); bundles.Add(new StyleBundle("~/main/css").Include( "~/assets/css/bootstrap.min.css", "~/assets/css/font-awesome.min.css", "~/assets/css/beyond.min.css", "~/assets/css/dataTables.bootstrap.css", "~/Content/toaster.min.css")); bundles.Add(new ScriptBundle("~/main/js").Include( "~/assets/js/skins.min.js", "~/assets/js/jquery.min.js", "~/assets/js/bootstrap.min.js", "~/assets/js/slimscroll/jquery.slimscroll.min.js", "~/assets/js/beyond.min.js", "~/assets/js/datetime/bootstrap-datepicker.js", "~/assets/js/datetime/moment.min.js")); } } }
To use bundling you can add to the Layout as shown below
<head> @Styles.Render("~/login/css") </head> <body> @Scripts.Render("~/login/js") </body>
As you can see, only one file loaded in the network tab when running the published website on IIS or run without debugging by setting debug is false in the web.config file.
system.web> <compilation debug="false" targetFramework="4.6.2"> </system.web>
Minification help you reduce file size by removing unnecessary white space and comments and shortening variable names to one character.
- Options Pattern In ASP.NET Core
- Implementing Rate Limiting in .NET
- IExceptionFilter in .NET Core
- Repository Pattern in .NET Core
- CRUD with Dapper in ASP.NET Core
- How to Implement Mediator Pattern in .NET
- How to use AutoMapper in ASP.NET Core
- How to fix 'asp-controller and asp-action attributes not working in areas'
Categories
Popular Posts
Stisla Admin Dashboard Template
11/18/2024
Admin Tailwind CSS Admin Dashboard Template
11/18/2024
Portal HTML Bootstrap
11/14/2024