


Exploring the little-known secrets of ASP.NET MVC (1): Support for LESS_html/css_WEB-ITnose
In ASP.NET MVC3 (from then on), we have the ability to bundle (Bundling) and compress (Minification) files such as js and css, which is the work of ASP.NET performance optimization part.
Think about it a long time ago, when we were in the mvc2 era, we introduced js and css files in this way:
<script type="text/javascript" src="@Url.Content("~/Script/Script.js")"></script>
There was no problem at first, As our projects grow larger and more resource files need to be introduced, we will inevitably encounter the following problems:
- These files need to be replaced in the Production environment For the compressed version (e.g jQuery.xxx.min.js)
- CSS files need to be compressed with other tools
- More and more files are introduced, which is difficult to manage
- A large number Resource files cause browsers to load slowly
After the release of ASP.NET MVC3, we no longer need to suffer from the above problems.
First of all, when we turn on the Optimizations switch, when System.Web/Compilation@debug in the Web.config file is set to false (in a production environment, it needs to be set to false), we browse The resource files obtained from the server will be compressed and bundled. The benefits of this are:
- Reduces server-side traffic and reduces server fever (through compression)
- Automatically caches resource files. If the server-side does not change, it will not be reloaded ( Through caching mechanism)
- Since most browsers limit the number of simultaneous connections to a host, the website access speed is accelerated (through bundling)
The following is a simple example, I Here is a demonstration using a newly created ASP.NET MVC project:
After creating a new MVC project, open the App_Start/BundleConfig.cs file and you can see a static RegisterBundles method. This method is the same as the RegisterRoutes method. They are all called when the application is loaded for the first time. That is to say, all bundling and compression operations will be performed once when the application is loaded, and will be directly referenced in the future. After the application is started, manual intervention is required. If a resource file is registered in the bundle, MVC will only reload and compress this part.
In the body of this RegisterBundles method, you can set the BundleTable.EnableOptimizations static property. This static property is set to True by default. That is, when the website is in a production environment, files such as css and js will be bundled and compressed. , these operations will not be performed in development mode. Of course, you can also organize this behavior by setting it to False.
In the method body, you can add the files we want to bundle by calling the Add method of the BundleCollection parameter. :
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.js", "~/Scripts/respond.js"));
The "~/bundles/bootstrap" here is a virtual path. When we reference it in the View file, what is actually loaded is "~/Scripts/ There are two files: "bootstrap.js" and "~/Scripts/respond.js". The parameter of the Include method is a string type parameter group, so we can add any number of files and apply them in the View file in the following way. :
@Scripts.Render("~/bundles/bootstrapr")
The bundling mechanism also supports CDN
var jqueryCdnPath = "http://libs.baidu.com/jquery/1.9.0/jquery.min.js"; bundles.Add(new ScriptBundle("~/bundles/jquery", jqueryCdnPath).Include( "~/Scripts/jquery-{version}.js"));
The point of this article is to directly use this bundling and compression mechanism to compile LESS. Although we can also use LESSCSS to compile LESS on the browser side, But we cannot get the compiled CSS file, and many times we need to get it to determine whether it is what we expected.
For more introduction to LESS and its advantages, see here
First, we need to add the dotless package:
After that, I In the Content directory of the project, add a StyleSheet1.less with the following code:
@color:#F7F7F7;body{ background-color:@color;}
At this time, you do not need to worry about IIS returning 404 to files with the suffix less, because The final output to the client is a compiled CSS file.
In the BundleRegister method, add the following code:
var lessbundle = new Bundle("~/bundles/less").Include("~/Content/*.less"); lessbundle.Transforms.Add(new LessTransform()); lessbundle.Transforms.Add(new CssMinify()); bundles.Add(lessbundle);
In the first line of code, we add all The suffix is the bundle of less files. The second line adds the LESS conversion function, which is provided by dotless. The third line adds the function of compressing this bundle, so that we will get the compilation in the Production environment. And compressed css code;
In a production environment, you will find code similar to the following in the source code obtained by the browser:
<link href="/bundles/less?v=vnAgv976RJi72MAy6iBw7DhQ9yxBbhXMXY0yOqNN5BU1" rel="stylesheet"/>
Open this path and you will Get the compiled CSS code:
body{background-color:#f7f7f7}
That’s it for this article. Things at work have been very disturbing recently. The mysophobia and sensitivity of technical people are so damn terrible. !

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using Windows Shello, a supported camera cannot be found. The common reasons are that the camera used does not support face recognition and the camera driver is not installed correctly. So let's take a look at how to set it up. Windowshello cannot find a supported camera tutorial: Reason 1: The camera driver is not installed correctly 1. Generally speaking, the Win10 system can automatically install drivers for most cameras, as follows, there will be a notification after plugging in the camera; 2. At this time, we open the device Check the manager to see if the camera driver is installed. If not, you need to do it manually. WIN+X, then select Device Manager; 3. In the Device Manager window, expand the camera option, and the camera driver model will be displayed.

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

Pros and cons of open source software: Understanding the pros and cons of open source projects requires specific code examples In today’s digital age, open source software is getting more and more attention and respect. As a software development model based on the spirit of cooperation and sharing, open source software is widely used in different fields. However, despite the many advantages of open source software, there are also some challenges and limitations. This article will delve into the pros and cons of open source software and demonstrate the pros and cons of open source projects through specific code examples. 1. Advantages of open source software 1.1 Openness and transparency Open source software

ASUS tufz790plus supports memory frequency. ASUS TUFZ790-PLUS motherboard is a high-performance motherboard that supports dual-channel DDR4 memory and supports up to 64GB of memory. Its memory frequency is very powerful, up to 4800MHz. Specific supported memory frequencies include 2133MHz, 2400MHz, 2666MHz, 2800MHz, 3000MHz, 3200MHz, 3600MHz, 3733MHz, 3866MHz, 4000MHz, 4133MHz, 4266MHz, 4400MHz, 4533MHz, 4600MHz, 4733MHz and 4800MHz. Whether it is daily use or high performance needs

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

How to use Flask-Babel to achieve multi-language support Introduction: With the continuous development of the Internet, multi-language support has become a necessary feature for most websites and applications. Flask-Babel is a convenient and easy-to-use Flask extension that provides multi-language support based on the Babel library. This article will introduce how to use Flask-Babel to achieve multi-language support, and attach code examples. 1. Install Flask-Babel. Before starting, we need to install Flask-Bab first.

Some users use the XP system and want to upgrade their graphics cards to gtx960, but are not sure whether gtx960 supports the xp system. In fact, gtx960 supports xp system. We only need to download the driver suitable for xp system from the official website, and then we can use gtx960. Let’s take a look at the specific steps below. Does gtx960 support XP system: GTX960 is compatible with XP system. Just download and install the driver and you're good to go. First, we need to open the NVIDIA official website and navigate to the home page. We then need to find a label or button above the page, it will probably be labeled "Drivers". Once we find this option we need to click on

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection
