This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24089|Reply: 8

[Source] ASP.NET Core (twelve) front-end JS, CSS bundling, and compression

[Copy link]
Posted on 2022-3-28 20:55:50 | | | |
Requirements: In previous .NET Framework MVC projects, JS, CSS can be bundled and compressed using Microsoft's Microsoft.AspNet.Web.Optimization library. After using ASP.NET Core, how do I bundle and compress existing JS and CSS files?

The .NET Framework project references the following:

Sample code:

binding

Bundling combines multiple files into a single file. Bundling reduces the number of server requests required to render a web asset, such as a web page. You can create as many individual bundles as you want specifically for CSS, JavaScript, and more. Fewer files mean from the browser to the server or from the service that provides the applicationFewer HTTP requests。 This improves homepage loading performance.

compress

Minify removing unnecessary characters from your code without changing functionality. The result wasThe size of requested resources, such as CSS, images, and JavaScript files, is significantly reduced。 Common side effects of minification include shortening variable names to one character and removing comments and unnecessary spaces. At the same time, it canConfuse the code, which is not conducive to other competitors reading our source code.

Performance gains from bundling and compression

The following table outlines the differences between loading assets individually and using bundling and minification:



It saves traffic and improves loading speed in terms of network transport, while also reducing HTTP requests and improving loading speed.

For HTTP request headers, the browser is very detailed. When bundled, the total bytes sent metric is significantly reduced. Load times show significant improvements, but this example runs locally. Bundling and minification can be combined with assets transported over the network to achieve higher performance gains.

ASP.NET Core does not have its own bundled and compressed solution, and needs to use a solution provided by a third party, this article uses "LigerShark.WebOptimizer.Core(The library eventually called.)NUglifyImplement JS and CSS handling), GitHub address:The hyperlink login is visible.

First, create a new project ASP.NET Core 6 and run the following command to reference:

Create a new static folder in the project wwwroot to store the test css and js files. Create a new CSS and JS file, as shown below:



Modify the Program.cs file and add the WebOptimizer service and middleware, the main code is as follows:

When we try to start the project, we find that the comments of both CSS and JS code have been deleted, and the files have been compressed, and some variables in JS have been reduced to a single letter, as shown in the figure below:



Then create a new js file from the static folder to test the bundling function.Bundle test.js and test2.js into a single common.js file(and does not generate a common.js physical file), the configuration code is as follows:

The renderings are as follows:



Reference:

The hyperlink login is visible.
The hyperlink login is visible.





Previous:C# 8.0 can be a null reference type
Next:[Actual combat]. NET 6 Random generates a random number test
 Landlord| Posted on 2022-3-28 20:56:47 |
Review:

ASP.NET Core (XI) endpoint route adds middleware to display all DI services
https://www.itsvse.com/thread-10269-1-1.html

ASP.NET Detailed explanation of Configuration priorities in Core(10).
https://www.itsvse.com/thread-10265-1-1.html

ASP.NET Detailed explanation of the Middleware middleware of Core (9).
https://www.itsvse.com/thread-9647-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
Posted on 2022-3-29 00:38:21 |
Learn to learn.
Posted on 2022-4-6 12:02:33 |
Ligershark.WebOptimizer.Core and WebMarkupMin.AspNetCore6 both tried
 Landlord| Posted on 2022-4-6 13:45:12 |
johnyoung Posted on 2022-4-6 12:02
Ligershark.WebOptimizer.Core and WebMarkupMin.AspNetCore6 both tried

Which one is better
Posted on 2022-4-6 14:53:12 |
This post was last edited by johnyoung on 2022-4-6 14:56

(I don't know how to delete duplicate comments, please delete this post when the webmaster sees it)
Posted on 2022-4-6 14:54:11 |

I have a preliminary understanding: the former is to bundle and compress js and css files, the latter is to compress page html and js and css code and http compression on the page, etc., and it is not clear whether the latter can bundle and compress js and css files. I just tried it and didn't use it in depth. I look forward to you exploring and posting another article.
 Landlord| Posted on 2024-4-24 15:35:15 |
Configure the development environment without compressing css and js, code:




 Landlord| Posted on 2024-10-10 11:28:22 |
The front-end uses Minify to compress and merge multiple CSS files
https://www.itsvse.com/thread-10845-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com