How to Bundling and Minification in ASP.NET MVC
By FoxLearn 2/18/2024 1:32:44 AM 42
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.
- How to fix "InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager'"
- How to fix 'IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson'
- ASP.NET Core: How to Create RDLC Report in ASP.NET Core
- ASP.NET MVC: Free Responsive Templates
- How to fix Assets file 'project.assets.json' not found
- How to fix 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
- How to set cookie ValidationInterval in ASP.NET Core
- How to fix LoginPath not working in ASP.NET Core
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024