Asp. NET component System.Web.Optimization principles and caching problems _ practical skills

Source: Internet
Author: User

1 "Introduction

This article will briefly analyze the operating principles and basic caching problems of the ASP.net component System.Web.Optimization for static file bundles:

There are a lot of static files in our project, in order to pursue the modular, plug-in many static files are designed into modules or decomposed, when needed in the form of a combination of the UI layer used; This brings a problem, the file will affect the browser load page speed, And because of the concurrency restrictions of browsers, the concurrent request is not unrestricted, so the function of bundling static files is generated; in fact, in the past, IIS did not integrate the pipeline model when we can only through the dynamic resources of the way to output, that is, we often see a lot of * in the *aspx page. Axd end of the request, of course, most of the case is with Asp.netajax used to output dynamic JS, htmdom, CSS use;

The latest IIS has been well integrated with the ASP.net piping model, which means that we can control all IIS requests, including static files, through the extension of ASP.net itself, making it possible to bundle static files;

Below we will analyze the basic operation principle of the System.Web.Optimization component, how it is loaded dynamically, how to control the cache;

2 "System.Web.Optimization components

Whenever we create a new ASP.NETMVC4 site will be in the ~/app_start directory has a BundleConfig.cs startup files, of course, create other asp.net4.0 and more than 4.0 of the project will have;

The first time I saw this document puzzled me, so I intend to analyze it briefly and know its rationale;

The code is a static method and then passes in a Bundlecollection collection object, which is actually a collection of bundle objects, and then registers multiple bundle into the collection; Each bundle corresponds to multiple static files, you can imagine that achievement is a set of key-value pairs ; The following include method contains n multiple static files, where the static file path can be a string that conforms to a particular rule and is computed from within it;

This is the registration phase, and then is the use phase, the use of the stage is very simple as long as we through our registered key string can directly reference these static file list;

We just focus on the Styles.render, Scripts.render two methods, which are a list of static files that are configured in the background before the page is injected, so what we see on the client is a collection of files that are bundled;

The connection address of the file is already the address after being bundled, this address is the key that we use before registering, it needs this key to obtain the value static file list If you want your bundle to work, you need to add a paragraph to the registration: Bundletable.enableoptimizations = true; code, meaning to open the bundle, if the bundle does not open the default in the debugging environment will not work, Because System.Web.Optimization uses the default bundle policy, if it is in debug mode, the bundle will not be enabled, and if you set it artificially it will override the default settings;

Use is this, we need to understand how it works, to understand its basic principles;

3 "System.Web.Optimization Component Fundamentals

Since IIS integrates the piping model, then we must be able to find the corresponding HttpModule, in order to save time I will not download the source code, we directly use the reverse compiler tool to look at;

This is bundle's HttpModule, which is only used to deal with
The bundle connection address, although it is in the HTTP pipeline; it's a good way to find it. But the weird thing is that I didn't find it in the Web.config configuration information, strange, it is also run to the system file change, of course, is impossible; so I can't think of any way to dynamically register, initiate dynamic registration suddenly have a thought, as if there is a assembly level of features used to register application _start boot code, will be implemented before the application start, to see;

It's hiding here. It registers a preapplicationstartcode static class that starts with the Start method;

This code is very simple, first of all to determine whether the registration, if not on the implementation of dynamic registration, this dynamic registration component is. The netframework is taken by itself, In the Microsoft.Web.Infrastructure is only a platform-related, and ASP.net has no direct relationship, we can use Microsoft.Web.Infrastructure to develop their own WEB components; Here's a question of why static methods should be added Broken, not only will be executed once, because the execution of static methods is not controlled, so if not to judge is likely to register many times, for serious consideration or add;

Now basically we have found the source, the server here we put, for the client a lot of questions, since it helped us bundle, then the cache is how to deal with, that is, its output cache is not set, if the set is not a problem;

"Client-side caching-related"

For a good understanding of the information between requests, we use Fiddler to monitor;

We see that the cache portion of it is using if-modified-since to represent the last modification of the local file, so that the server can verify that the file changes, if the server's response status code is not changed 304, Description bundle in the output of the time does not set the file for the client forced caching, we can see through the Pragma:no-cache head;

Then we come to the conclusion that all bundle out of the file can not be directly cached in the browser, each time with the cache segment if-modified-since to verify the server file version; Just here we can with the dynamic output of the static file address of the following parameters;

Like what:

/content/css?v=zpnwvrt3c0yyrvdpmi-xkjuhbdjfqsl3a0k5c9wtok01

The v parameter behind this link is the version of the virtual file after the current bundle, and if we modify the file on the server then the if-modified-since verification of the file fails, and a new version number is generated as the connection parameter; Let's take a look at the heart;

I added a width:auto style, so this time I refresh the client should be no more than 304 appeared;

Obviously the version of/content/css?v=doyfok3bdoywdirbq7juv6eqdljau6rtc0g13el7x041 file changed, then response should not be 304;

If the version number of the static file changes, it will not bring the if-modified-since, this is used in each of the post is used to verify; in fact, it means that if you do not have IIS Integrated mode, the way to bundle files can only change the file name of the static file;

4 "Extend custom type static files

The bundle object is the base class for all the files that need to be bundled, and if we need to extend some static files, such as static files in certain fields, we can inherit the class directly;

"Caching of XML Files"

Extended XML file is very simple, we only need to inherit the bundle object, all about the dynamic generation of URLs have a special object processing, we look at the code;

public class Xmlbundle:bundle
 {public
 Xmlbundle (string path): Base (Path) {}
 } ' public
 static class Xml Bundlerender
 {public
 static ihtmlstring Render (string path)
 {
  Bundleresolver bundle = new Bundleresolver (bundletable.bundles);
  return new Htmlstring (
  string. Format (@ "<link href=" "{0}" "Rel=" "Stylesheet" "/>", bundle.) Getbundleurl (path)));
 }
 

First we need a xmlbundle that inherits from the bundle object to represent all the XML file bundles we are going to transfer, and then we need a static method to register the bundled URL;

The creation of this URL has a special Bundleresolver object to complete, we only need to pass in all the Bundlecollection objects, I here in order to be able to test in the browser, so I wrote a stylesheet type of link So we can use it directly where we need it, and I'll refer to it in the index view: @MvcApplication4. Seed.XmlBundleRender.Render ("~/custom/xml"); This allows us to bundle all the files that we want to control, simply by inheriting the addition of a simple static method;

Let's see if our XML file has all the caching features;

The first request did not add if-modified-since paragraph, the return content is a simple <model>222</model> test simple, now we see whether it is the next time does not change the content of the use of the cache;

In our expectation, using the cached data, we need to modify the XML file on the server, change 222 to 243454637 to see if the local cache is automatically refreshed that is to say it will not be 304 return status;

Also refreshes the cache, conforms to the theory basis, correctly returns our modified value;

Knot: In fact, HTTP is not only used in the browser, there will be a lot of use of HTTP occasions, so we can be very good for this function to bundle some pictures, text and other occasions, is indeed a good component; the end of the article, thank you;

Author: Qingyue

Source: http://www.cnblogs.com/wangiqngpei557/

This article copyright to the author and blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original connection, otherwise retain the right to pursue legal responsibility.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.