How to Bundling and Minification in ASP.NET MVC
By FoxLearn 2/18/2024 1:32:44 AM 210
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 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
- How to fix Can't find Request.GetOwinContext in Web API
- How to fix Can't use Server.MapPath in ASP.NET MVC
- Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager'
- ASP.NET MVC Responsive Templates Free Download
- How to upload file in ASP.NET MVC
- How to Create Contact Form Flat Responsive in ASP.NET MVC
- HTTP Error 500.30 ASP.NET Core app failed to start
Categories
Popular Posts
Material Lite Admin Template
11/14/2024
Freedash bootstrap lite
11/13/2024
RuangAdmin Template
11/17/2024
Responsive Animated Login Form
11/11/2024