MVC4 added some new features, and next, we'll look at what's new in the next MVC4, and when we create a new MVC4 project, we'll find a App_start folder under the project, with 4 files under the folder, Bundleconfig.cs,filterconfig.cs,routeconfig.cs,webapiconfig.cs, where the BundleConfig.cs file is the document we are talking about in this verse.
It is well known that the number of file connections requested is limited when the browser sends a request to the server. The use of Bundleconfig can be multiple file requests and and into a request, remove some comments in the file, blank, compressed file size, automatically merge compression optimization code, shorten the response time, improve the speed of the web, play to optimize the role of the site.
1. After defining a group
to create a MVC4 project, we see the following code in the Application_Start () method in the Global.asax file, which registers the role
Bundleconfig.registerbundles (Bundletable.bundles);
BundleConfig.cs Code:
public class Bundleconfig {//bundling for more information, please visit http://go.microsoft.com/fwlink/? linkid=254725 public static void Registerbundles (Bundlecollection bundles) {bundles. ADD (New Scriptbundle ("~/bundles/jquery").
Include ("~/scripts/jquery-{version}.js")); Bundles. ADD (New Scriptbundle ("~/bundles/jqueryui").
Include ("~/scripts/jquery-ui-{version}.js")); Bundles. ADD (New Scriptbundle ("~/bundles/jqueryval").
Include ("~/scripts/jquery.unobtrusive*", "~/scripts/jquery.validate*")); Develop and understand information using the Modernizr development version.
Then, when you are ready to prepare//production, use the build tools on the http://modernizr.com to select only the tests you want. Bundles. ADD (New Scriptbundle ("~/bundles/modernizr").
Include ("~/scripts/modernizr-*")); Bundles. ADD (New Stylebundle ("~/content/css").
Include ("~/content/site.css")); Bundles. ADD (New Stylebundle ("~/content/themes/base/css").
Include ("~/content/themes/base/jquery.ui.core.css", "~/content/themes/base/jquery.ui.resizable.css", "~/content/themes/base/jquery.ui.selectable.css", "~/content/themes/base/jquery.ui.accordion.css", "~/Con Tent/themes/base/jquery.ui.autocomplete.css "," ~/content/themes/base/jquery.ui.button.css "," ~/Content/theme S/base/jquery.ui.dialog.css "," ~/content/themes/base/jquery.ui.slider.css "," ~/content/themes/base/jquery.ui . Tabs.css "," ~/content/themes/base/jquery.ui.datepicker.css "," ~/content/themes/base/jquery.ui.progressbar.c
SS "," ~/content/themes/base/jquery.ui.theme.css ")); }
}
The
Bundlecollection is a collection that is used to add binding rules to the collection, using the Add method.
Bundles. ADD (New Scriptbundle ("~/bundles/jquery"). Include ("~/scripts/jquery-{version}.js"));
New Scriptbundle ("~/bundles/jquery") defines a grouping name, which includes the files that are specifically included under this grouping. The
version parameter represents the build number, * represents all, and these two are understood as wildcard characters.
The above code represents the creation of a ~/bundles/jquery grouping that points to files that are jquery-1.7.1.js files under the Scripts folder.
If there are jquery-1.7.1.js,jquery-1.6.1.js under the Scripts folder, the two files will be pointed to.
Similarly, bundles. ADD (New Stylebundle ("~/content/css"). Include ("~/content/site.css")); indicates that a ~/content/css group was created, pointing to the ~/content/site.css file.
2. Use grouping
Grouping is already defined, and we'll take a demonstration of how to use it.
If you use Scripts on a page, you can use the @scripts.render ("~/bundles/jquery") method, and the argument is a grouping name
if you use CSS on a page, you can use @styles.render ("~/ Content/css ") method, the parameter is a grouping name.
You can see the _layout.cshtml file for the Shard folder under the Views folder
After you run the project, you can view the page source code, which works as follows
3. Consolidated request
Next, we will request multiple files to be merged into a request by grouping, and we can use the following two methods to implement
1). Set debug for compilation debugging in Web.config to false <compilation debug= "false" targetframework= "4.5"/>
2. Add bundletable.enableoptimizations = True at the end of the method in Bundleconfig;
Run the page again and you can see the following code generated
src= "/bundles/jquery?v=wbuqtimtmgl9hj0haqmerbd8com3uagnawp4udekfnm1"
? Preceded by a grouping name followed by a hash code generated after merging multiple files
4. Contrast effect
We use Firebug under Firefox to see the contrast effect before and after the merge, you can compare the file size requested, time and other aspects of the difference is quite large
Before compressing the merge:
After compression merge:
Note: By default bundletable.bundles will filter out files with suffix names, intellisense.js,-vsdoc.js,. debug.js,. Min.js,. Min.css,
When the file with the suffix name is loaded, a blank is displayed.
You can remove filter restrictions on these files in the following ways
BundleTable.Bundles.IgnoreList.Clear ();
BundleTable.Bundles.IgnoreList.Ignore (". Min.js", optimizationmode.always);
BundleTable.Bundles.IgnoreList.Ignore ("-vsdoc.js", optimizationmode.always);
BundleTable.Bundles.IgnoreList.Ignore (". Debug.js", optimizationmode.always);
By grouping, we request multiple files to be merged into a request, compress the file size, automatically merge and compress the optimized code, shorten the response time, and improve the speed of the Web page. Hope that through this article can help you better optimize the site.