基於Asp.Net MVC4 Bundle捆綁壓縮技術的介紹

來源:互聯網
上載者:User

很高興,最近項目用到了Asp.Net MVC4 + Entity Framework5,發現mvc4加入了Bundle、Web API等技術,著實讓我興奮,以前是用第三方的,這裡主要說說Bundle技術。

很多大網站都沒有用Bundle技術造成很多資源浪費與效能的犧牲,別小瞧 用上了你會發現他的好處:

將多個請求捆綁為一個請求,減少伺服器請求數

沒有使用Bundle技術,debug下看到的是實際的請求數與路徑

使用Bundle技術,並且擁有緩衝功能
調試設定為Release模式並按F5或修改web.config,就可以看到合并與壓縮的效果

壓縮javascript,css等資源檔,減小網路頻寬,提升效能

後台配置

  MVC4在架構上有些變動,簡化了原來的Global.asax,增加了一些靜態設定檔在App_Start下面,留意下BundleConfig.cs,顧名思義是Bundle的配置,所有它的配置在這裡進行就可以了,當然也可以單獨的設定檔。

複製代碼 代碼如下: public class BundleConfig { // For more information on Bundling, 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*")); // Use the development version of Modernizr to develop with and learn from. Then, when you're // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. 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", "~/Content/themes/base/jquery.ui.autocomplete.css", "~/Content/themes/base/jquery.ui.button.css", "~/Content/themes/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.css", "~/Content/themes/base/jquery.ui.theme.css")); } }

這裡大家可以按模組化去配置,我們看到的下面的Url對應的就是上面的bundles.Add(...) 所增加的js、css的virtualPath

需要注意的是不同virtualPath 增加的相同的資源檔,會被重複載入!

前台調用

對於公用的資源檔,通常我們都會放到_Layout.cshtml (webform中的母板頁) 檔案中

  Script檔案引用:@Scripts.Render(virtualPath[,virtualPath1][,virtualPath2][,...])
   CSS檔案引用: @Styles.Render(virtualPath[,virtualPath1][,virtualPath2][,...])

複製代碼 代碼如下:@Styles.Render("~/Content/css") @Styles.Render("~/Content/themes/base/css")...

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") @RenderSection("scripts", required: false)

正則匹配需要的,過濾不需要的

複製代碼 代碼如下: bundles.IgnoreList.Clear(); bundles.IgnoreList.Ignore("*.debug.js"); bundles.IgnoreList.Ignore("*.min.js"); bundles.IgnoreList.Ignore("*-vsdoc.js"); bundles.IgnoreList.Ignore("*intellisense.js"); bundles.Add(new ScriptBundle("~/bundles/jquery", jqueryCdn).Include( "~/Scripts/jquery-{version}.js")); //匹配jquery版本    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.unobtrusive*", //匹配檔案名稱首碼為jquery.unobtrusive "~/Scripts/jquery.validate*")); ...

使用CDN

複製代碼 代碼如下: bundles.UseCdn = true; //使用CDN string jqueryCdn = "http:deom.jb51.net/jslib/jquery/jquery-1.7.1.min.js"; bundles.Add(new ScriptBundle("~/bundles/jquery", jqueryCdn).Include( "~/Scripts/jquery-{version}.js"));

當cdn伺服器掛了或不能訪問了,這裡就會選擇本地的資源檔,debug下mvc 會讓我們看到他原來的面具,這點非常好利於我們調試。  
   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.