


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>Why
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"));Focus came
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}In the Production environment, go one step further and remove the blank characters in the css to make the CSS file smaller, because my example is actually They're the same, so you can't tell the difference.
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. !

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version