. net/asp.net 4.5 Bundle Component (bundle, shrink static file)

Source: Internet
Author: User
Tags key string

Read the catalogue:

    • 1. Introduction
    • 2.system.web.optimization components
    • Fundamentals of 3.system.web.optimization Components
    • 4. Extending the custom type static file
1 "Introduction

This article will be a simple analysis of the static file bundle of ASP. System.Web.Optimization operation principle and basic caching problems;

In our project is filled with a lot of static files, in order to pursue a modular, plug-in a lot of static files are designed to block the way or be broken down, when needed in a combination of the way on the UI layer, which brings a problem, more files will affect the speed of the browser to load the page, And because of the browser concurrency limit, for parallel requests is not unlimited, so the function of binding static files is generated; in fact, before IIS had no integrated pipeline model, we could only output it through dynamic resources, that is, we often see a lot of * in the *aspx page. Axd end of the request, of course, in most cases with asp.netajax used to output dynamic JS, htmdom, CSS used;

The latest IIS has a good integration of the ASP. NET pipeline model, which means that we can control all requests through IIS, including static files, through the extension of the net itself, so it is possible to bundle static files;

Below we will analyze the basic operating principle of the System.Web.Optimization component, how it is dynamically loaded, 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 file, of course, create other asp.net4.0 and more than 4.0 of the project will also have;

The first time I saw this file was confusing to me, so I'm going to simply analyze it 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 by registering multiple bundles within the collection, each bundle corresponds to multiple static files, and you can imagine that the achievement is a set of key-value pairs The following include method contains more than n static files, where the static file path can be a string that conforms to a particular rule, which is computed internally;

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

As long as we focus on Styles.render, Scripts.render two methods, these two methods are to want to page injection before the background configuration of the static file list, so what we see on the client is the bundled file collection;

The file's connection address is already a bundled address, this is the key we used to register before, it needs this key to get the value of the static file list If you want to bind the effect need to add a paragraph when registering: Bundletable.enableoptimizations = true; code, meaning to say to open the bundle, if you do not open the bundle, the default in the debugging environment will not effect, Because System.Web.Optimization uses the default bundle policy, if it is in debug mode, the bundle will not be enabled, if you set it artificially will override the default settings;

The use is these, the following we need to understand how it works, to understand its rationale;

3 "System.Web.Optimization Component Fundamentals

Since IIS integrates the pipeline model, then we must be able to find the corresponding HttpModule, in order to save time I will not download the source, we directly with the Anti-compilation tool to look at;

This is the bundle's HttpModule, which is used only to handle
The connection address of the bundle, although it is in the pipeline of HTTP; it's a good way to find it. But the strange thing is that I didn't find its configuration information in Web. config, it's weird, it's not going to go to the system file change, of course, it is not possible; so I can't think of any way to do it. Dynamic registration, dynamic registration suddenly have a thought, as if there is a assembly level of features to register application _start boot time of the pre-code, will be executed before application start, to look at;

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

This code is very simple, first of all to determine whether there is a registered, if not to perform dynamic registration, this dynamic registration component is. NETFramework comes with, In the Microsoft.Web.Infrastructure is only platform-related, and ASP. NET, we can use Microsoft.Web.Infrastructure to develop our own WEB components; Here's a question of why static methods also need to be awarded Broken, not only do it once, because the execution of static methods is not controlled, so if not judged very likely to register many times, for serious consideration or add;

Now basically we have found the source, the server here we first put, for the client's question a lot, since it helps us bundle, then the cache is how to deal with, that is, its output cache has no settings, if the setting is not a problem;

"Client Cache related"

In order to get a good understanding of the information between requests, we use Fiddler to monitor;

We see that the cache part uses If-modified-since to represent the last modification of the local file, so that the server can verify that the file has changed, and if the server's response status code is 304, The bundle does not set the client force cache for this file when it is output, and we can see it through the Pragma:no-cache header.

Then we conclude that all bundles of files can not be cached directly in the browser, each time with the cache segment if-modified-since to verify the server version of the file, just here we could with the dynamic output of the static file address after the parameter pair up;

Like what:

/content/css?v=zpnwvrt3c0yyrvdpmi-xkjuhbdjfqsl3a0k5c9wtok01

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

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

Obviously the version of the/content/css?v=doyfok3bdoywdirbq7juv6eqdljau6rtc0g13el7x041 file has changed, so 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 to verify, in fact, it means that if there is no IIS integration mode, the way to bundle files can only change the file name of static files;

4 "Extended Custom type static file

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

"Caching of XML Files"

The extension XML file is very simple, we only need to inherit the bundle object, all about the dynamically generated URL has a dedicated object processing, we look at the code;

View Code

First we need a xmlbundle that inherits from the bundle object to represent all of our XML file bundle containers that will be transferred, and then we need a static method to register the bundled URL;

This URL generation has a dedicated Bundleresolver object to complete, we only need to pass in all the Bundlecollection object, I am here in order to be able to test in the browser so wrote a stylesheet type of link So that we can directly use it directly where we need it, and I quote in the index view: @MvcApplication4. Seed.XmlBundleRender.Render ("~/custom/xml"); So that we can bind all the files that want to control the bundle, only need to inherit the simple static method assistance;

Let's take a look at whether our XML file has all the caching features;

The first request did not add if-modified-since segment, the content returned is a simple <model>222</model> test simple, now we see whether it is the next time the content is not changed in the case of the use of the cache;

When we expected to use the cached data, we need to change the XML file on the server, change 222 to 243454637 to see if the local cache is automatically refreshed that is not 304 return status;

Also refresh the cache, in line with the theoretical basis, the correct return of our modified values;

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 to bundle some pictures, text and other occasions, is really a good component; the end of the article, thank you;

. net/asp.net 4.5 Bundle Component (bundle, shrink static file)

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.