A feature of the MVC framework in the .NET Framework called Bundling enables programmers to merge several CSS and JavaScript files into a single file. As fewer requests are needed to load the page, this can enhance the performance of web applications.
In order to use the bundling feature, one or more CSS or Java Script files are logically grouped together to form a bundle object. The files that should be included in the bundle are specified by the bundle object, together with any options for caching, minification, and other configurations.
The bundling functionality automatically unbundles all of the files in each bundle into a single file and sends it to the client when the application is executed. This lessens the quantity of HTTP requests required to load the page and can improve the performance of the application. Check out the .NET online training to learn more.
Some of the key features of bundling in the MVC framework include:
- Minification: Minification, a technique for reducing the size of CSS and JavaScript files by removing superfluous characters, is supported by the bundling function. This can decrease the quantity of data that needs to be carried over the network, which can enhance the application’s performance.
- Caching: Bundling comes with caching capability, enabling the client’s browser to keep a local copy of the packaged file. This can lessen the amount of HTTP requests needed to load the page, which can enhance application performance.
- Customization: By building their own bundles and supplying their own options for caching, minification, and other parameters, developers can personalise the bundling functionality. This enables programmers to adjust an application’s performance to suit their own requirements.
All things considered, the MVC framework’s bundling capability is a potent tool for enhancing online application performance. Bundling can enhance user experience and increase application responsiveness and efficiency by lowering the number of HTTP requests needed to load the page.
Bundling and Minification are two features that the MVC offers us to help lower the number of requests sent to the server to retrieve the resource file.
One of MVC’s features is bundling. This can be put into practice to speed up performance request load times. The technique of deleting extraneous information without affecting its operation, such as comments and white spaces, or shrinking huge variable names, is known as minification. It reduces the file size so that loading time is reduced.
The bundling technique in ASP.NET MVC is provided by the System.Web.Optimization namespace and is found in the Microsoft.Web.Optimization assembly.
You must include this DLL as a reference in your project using System.Web.Optimization in order to implement bundling in the application.
“Improve the performance response and request load time of web pages” is the stated aim of bundling.
All that the bundle does is collect the files into a single entity. Assume that our application has several CSS files, such as Bootstrap and others, which we can link together as a single unit.
Let’s look at the coding side. If you choose the MVC template while establishing a project for an ASP.NET MVC Web Application, bundle file configuration is automatically generated.
After Project create, You can check under,
- App_start folder: You’ll find the BundleConfig file in which all script and style bundles are defined.
- web.config file: You’ll see the debug mode is set to true.
Take note that the compilation debug mode is now set to true. our implies that the compiler will bundle or minify our CSS and JavaScript files when our application executes. It won’t bundle the files if it isn’t set to true, or; You can override this Web.config setting with the EnableOptimizations property on the BundleTable class in Global.asax file.
Advantages
- Bundling facilitates the download of the same kind of files with a single request as opposed to several.
- Rather than retrieving every resource file (CSS and JS) individually, we can collect resources into a bundle and retrieve the bundle in a single request.
- Minification contributes to file size reduction and enhanced request timeliness.
- Bundling automatically handles this problem by adding a hashcode to each bundle as a query parameter in the URL. Whenever you make changes to the contents of the JS and CSS files, a new hash code is generated and automatically rendered on the page. This resolves the problem of server change files not being reflected on the browser.
Conclusion To learn more about MVC Framework Bundling, check our online .NET course.