標籤:site 完整版 詳解 英文 http asc conf 查看 tool
一、簡述
Bundle,英文原意就是捆、收集、歸攏。在MVC中的Bundle技術,也就是一個對css和js檔案的捆綁壓縮的技術。
它的用處:
將多個請求捆綁為一個請求,減少伺服器請求數
壓縮javascript,css等資源檔,減小網路頻寬,提升效能
使用Bundle技術,並且擁有緩衝功能,同時也可以對資源檔進行一定的模組化管理,可使用正則對需要的檔案進行過濾匹配,也可以使用cdn檔案
二、技術詳解1.怎麼開啟bundle
在項目的App_Start檔案夾中,會有一個BundleConfig.cs檔案;
public class BundleConfig { // For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js")); bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.validate*")); // 使用要用於開發和學習的 Modernizr 的開發版本。然後,當你做好 // ready for production, use the build tool at https://modernizr.com to pick only the tests you need. bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.js", "~/Scripts/respond.js")); bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/bootstrap.css", "~/Content/site.css")); BundleTable.EnableOptimizations = true; //是否打包壓縮 } }
對Bundle的註冊是在項目根下的Global.asax檔案中,這個檔案中的Application_Start是網站程式的開始,裡面註冊了網站各種初始化的內容,其中就包括對BundleTable的Bundle添加:BundleConfig.RegisterBundles(BundleTable.Bundles);
預設情況下,Bundle是會對js和css進行壓縮打包的,不過有一個屬性可以顯式的說明是否需要打包壓縮。
BundleTable.EnableOptimizations = true;
配置web.config,關掉調試狀態,否則不會進行壓縮。
<system.web> <compilation debug="false" targetFramework="4.5.2" /> </system.web>
2.如何使用
視圖中調用方法:
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/bootstrap")
捆綁架構如以下幾個共同的約定:
- 如果“FileX.min.js”和“FileX.js”都存在,請為發行版選擇“.min”檔案。
- 選擇用於調試的非".min"版本。
- 忽略"-vsdoc"僅使用智能感知的檔案 (如 jquery-1.7.1-vsdoc.js)。
如上所示的{version}萬用字元匹配用於自動建立一個 jQuery 束具有適當版本的 jQuery指令碼檔案夾中。在此樣本中,使用萬用字元提供了以下好處:
- 允許您使用 NuGet 更新到新的 jQuery 版本而無需更改前面的綁定代碼或 jQuery 引用在您查看網頁。
- 自動選擇完整版,為調試配置和發布的".min"版本產生。
3.使用 CDN
以下代碼將使用 CDN jQuery 綁定來替換本地 jQuery 綁定。
public static void RegisterBundles(BundleCollection bundles){ bundles.UseCdn = true; //enable CDN support //add link to jquery on the CDN var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"; bundles.Add(new ScriptBundle("~/bundles/jquery",jqueryCdnPath).Include("~/Scripts/jquery-{version}.js"));}
在上面的代碼中,jQuery 將請求從 CDN 雖然在釋放模式和 jQuery 的調試版本將被回遷本地在偵錯模式下。當使用 CDN,你應該有一個回退機制在 CDN 請求失敗的情況下。下面的標記片段從布局檔案的末尾顯示指令碼添加請求 jQuery 應 CDN 失敗。
@Scripts.Render("~/bundles/jquery") <script type="text/javascript"> if (typeof jQuery == ‘undefined‘) { var e = document.createElement(‘script‘); e.src = ‘@Url.Content("~/Scripts/jquery-1.7.1.js")‘; e.type = ‘text/javascript‘; document.getElementsByTagName("head")[0].appendChild(e); } </script> @RenderSection("scripts", required: false)
三、常見問題1. css中引用的圖片路徑出現錯誤
在css中,圖片路徑一般都是寫相對路徑,使用bundle後出現錯誤。處理方法:通過 new CssRewriteUrlTransform() 來解決
bundles.Add( new StyleBundle("~/Content/login") .Include("~/Content/login.css", new CssRewriteUrlTransform()) );2. css中使用@Import "base.css" 找不到對應的檔案
解決:修改為 @import url("base.css");
import的相關文章:1190000000369549
參考:
http://blog.csdn.net/zhou44129879/article/details/16818987
http://www.cnblogs.com/weishao/archive/2013/04/12/3015005.html
第五章 MVC之Bundle詳解