Starting with MVC4, we found that Global.asax was optimized in the project to move the code originally used in MVC3 to the "App_start" folder, and Global.asax was only responsible for initializing it. The Bundleconfig class has a function of a bull x: Merge and Compress. Think of doing ASP before. NET time to be compressed by the tool, manually merged, very cumbersome. The bundleconfig can greatly improve work efficiency and project performance.
First, the basic use
1.1. Initialization of Global.asax files
1234 |
protected void Application_Start() { RouteConfig.RegisterRoutes(RouteTable.Routes); } |
1.2. bundleconfig Binding Compressed file
1234567891011121314 |
public
class BundleConfig
{
// 有关 Bundling 的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=254725
public
static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(
new ScriptBundle(
"~/bundles/jquery"
).Include(
"~/Content/Scripts/jquery-{version}.js"
));
bundles.Add(
new ScriptBundle(
"~/Content/Scripts/toojs"
).Include(
"~/Content/Scripts/jquery.cookie.js"
,
"~/Content/Scripts/footer.js"
));
|
1.3. Display in view
123 |
@Styles.Render( "~/Content/Css/Common" ) //单个样式的绑定 @Scripts.Render( "~/bundles/jquery" , "~/Content/Scripts/toojs" , "~/Content/Sctipts/bootstraptJs" ) //多个JS的绑定 |
1.4. Configuration of Web. config
12 |
<system.web> <compilation debug= "true" targetFramework= "4.5" /> |
Second, the effect explanation
When compression is enabled, open Firebug you will see
Multiple files are merged together, the file format is removed, and the resulting compression increases the load time of the file.
I think there's another very smart benefit: support for regular matching files
*\{version} Two is a good match, in the actual project, in the style iterative development, stlye.1.0.css,stlye.1.1.css,stlye.1.2.css,stlye.1.3.css ... The pattern accumulates, the path only writes ~/content/stlye.*.css to be possible.
In the JS development time changed a lot of time Bug,js have version update: Script.1.0.js,script.1.0.min.js,script.1.1.js,script.1.1.min.js will use to the latest version, with {version} The latest files will be taken in debug mode, and the latest min will be taken when you publish.
Three, the attention matters:
1 |
刚开始的时候虚拟路径的命名有就很奇怪: ~/bundles/jquery。如下: |
1 |
bundles.Add( new ScriptBundle( "~/bundles/jquery" ).Include( |
1 |
bundles.Add( new ScriptBundle( "~/Content/Scripts/toojs" ).Include(<br><br> |
1 |
我开始以为:~/Content/Scripts 是文件的文件夹地址,后面随便命名,但~/bundles 又是什么?我们跟踪的时候发现: |
1 |
在bundles 注册之前就有了值,原来系统已经定义好了7个路径标识,应该是默认常用的吧。这个地方坑了我好长时间了,百度也没人说~ |
Well, that's all, this verse doesn't say the code, that's the new feature under the bundle.
NET MVC application Bundle (bundle and micro) compression technology enable Bundleconfig configuration Web. config