很高興,最近項目用到了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")); } }新聞熱點
疑難解答
圖片精選