2017年11月1日 星期三

[.Net MVC] 使用MVC BundleConfig的{version} wildcard的注意事項


使用ASP.Net MVC預設範本的BundleConfig,
會使用{version}來bundle jQuery套件,如:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));


之前看微軟文件, 使用{version}萬用字元的好處其一是:
  • debug組態:會自動bundle full debug版本。
  • release組態:就bundle min版本。
看起來也很直覺。

但有一次在release組態,我將full debug(jquery-x.x.x.js)的版本拿掉,
發現jquery沒有正確載入,才發現MVC預設會忽略min版本(in the bundles.IgnoreList),
所以應是使用full debug版本bundle與minify,
再加入HttpRuntime.Cache,如下圖紅框處。



因此,在Scripts資料夾下放full debug版本的js或css應該就沒問題了。
to be continued...


參考資料:
https://stackoverflow.com/questions/29254181/bundling-not-working-in-mvc5-when-i-turn-on-release-mode
https://docs.microsoft.com/en-us/aspnet/mvc/overview/performance/bundling-and-minification
https://stackoverflow.com/questions/21270834/asp-net-mvc-bundle-not-rendering-script-files-on-staging-server-it-works-on-dev
http://www.mytecbits.com/microsoft/dot-net/asp-net-mvc-bundle-rendering
http://demo.tc/post/779