在Global.asax中添加两行代码
//默认在调试期间,不会启用js和css的压缩
//下面的语句确保了在调试期间也压缩css和js
BundleTable.EnableOptimizations = true;
BundleConfig.RegisterBundles(BundleTable.Bundles);
可以保证在调试中也压缩js和css代码
在BundleConfig中进行js和css的压缩
using System.Web.Optimization; namespace FuturesContest.UI
{
public class BundleConfig
{ // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
#region 后台layout
bundles.Add(new ScriptBundle("~/bundles/layoutJs").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/bootstrap.min.js",
"~/Content/vendors/fastclick/lib/fastclick.js",
"~/Content/vendors/nprogress/nprogress.js",
"~/Content/vendors/bootstrap-progressbar/bootstrap-progressbar.min.js",
"~/Content/vendors/moment/moment.js",
"~/Content/vendors/sweetalert/sweetalert2.min.js",
"~/Scripts/Gentelella/custom.js")); bundles.Add(new StyleBundle("~/Content/layoutCss1").Include(
"~/Content/bootstrap.min.css")); bundles.Add(new StyleBundle("~/Content/Gentelella/layoutCss2").Include(
"~/Content/Gentelella/custom.min.css")); bundles.Add(new StyleBundle("~/Content/vendors/nprogress/layoutCss3").Include(
"~/Content/vendors/nprogress/nprogress.css"
));
bundles.Add(new StyleBundle("~/Content/vendors/sweetalert/layoutCss4").Include(
"~/Content/vendors/sweetalert/sweetalert2.min.css"
)); bundles.Add(new StyleBundle("~/Content/vendors/font-awesome/css/layoutCss5").Include(
"~/Content/vendors/font-awesome/css/font-awesome.min.css"
));
bundles.Add(new StyleBundle("~/Content/vendors/bootstrap-progressbar/css/layoutCss6").Include(
"~/Content/vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css"
)); #endregion #region 前台layout
bundles.Add(new ScriptBundle("~/game/layoutJs").Include(
"~/Scripts/jquery-{version}.js",
"~/Content/vendors/sweetalert/sweetalert2.min.js",
"~/Content/vendors/moment/moment.js",
"~/Scripts/bootstrap.min.js")); bundles.Add(new StyleBundle("~/Content/Game/layoutCss1").Include(
"~/Content/Game/comment.css",
"~/Content/Game/responsive.css",
"~/Content/Game/themes.css")); #endregion #region validate
bundles.Add(new ScriptBundle("~/bundles/validateJs").Include(
"~/Content/vendors/poshytip-1.2/src/jquery.poshytip.min.js",
"~/Scripts/jquery.validate.min.js",
"~/Scripts/jquery.validate.unobtrusive.js",
"~/Scripts/common.js",
"~/Scripts/jquery.unobtrusive-ajax.min.js")); //压缩失效
bundles.Add(new StyleBundle("~/Content/vendors/poshytip-1.2/src/tip-yellow/validateCss").Include(
"~/Content/vendors/poshytip-1.2/src/tip-yellow/tip-yellow.css"));
#endregion #region datatables bundles.Add(new ScriptBundle("~/bundles/datatablesJs").Include(
"~/Content/vendors/datatables.net/js/jquery.dataTables.min.js",
"~/Content/vendors/datatables.net-bs/js/dataTables.bootstrap.min.js",
"~/Content/vendors/iCheck/icheck.min.js",
"~/Scripts/datatables.helper.js")); bundles.Add(new StyleBundle("~/Content/vendors/datatables.net-bs/css/datatablesCss1").Include(
"~/Content/vendors/datatables.net-bs/css/dataTables.bootstrap.min.css")); bundles.Add(new StyleBundle("~/Content/vendors/iCheck/skins/flat/datatablesCss2").Include(
"~/Content/vendors/iCheck/skins/flat/green.css")); #endregion #region ueditor bundles.Add(new ScriptBundle("~/bundles/ueditorJs").Include(
"~/Content/vendors/ueditor1_4_3_3-utf8-net/utf8-net/ueditor.config.js",
"~/Content/vendors/ueditor1_4_3_3-utf8-net/utf8-net/ueditor.all.js",
"~/Content/vendors/ueditor1_4_3_3-utf8-net/utf8-net/lang/zh-cn/zh-cn.js",
"~/Content/vendors/ueditor1_4_3_3-utf8-net/utf8-net/ueditor.parse.js")); bundles.Add(new StyleBundle("~/Content/vendors/ueditor1_4_3_3-utf8-net/utf8-net/themes/ueditorCss").Include(
"~/Content/vendors/ueditor1_4_3_3-utf8-net/utf8-net/themes/iframe.css"));
#endregion #region fileinput bundles.Add(new ScriptBundle("~/bundles/fileinputJs").Include(
"~/Content/vendors/bootstrap-fileinput-master/js/fileinput.min.js",
"~/Content/vendors/bootstrap-fileinput-master/js/locales/zh.js",
"~/Scripts/fileHelper.js"
)); bundles.Add(new StyleBundle("~/Content/vendors/bootstrap-fileinput-master/css/fileinputCss").Include(
"~/Content/vendors/bootstrap-fileinput-master/css/fileinput.min.css"));
#endregion #region my97 bundles.Add(new ScriptBundle("~/bundles/my97Js").Include(
"~/Content/vendors/My97DatePicker/WdatePicker.js",
"~/Content/vendors/My97DatePicker/lang/zh-cn.js"
)); bundles.Add(new StyleBundle("~/Content/vendors/My97DatePicker/skin/default/my97Css").Include(
"~/Content/vendors/My97DatePicker/skin/default/datepicker.css"));
#endregion }
}
}
注意:有些JS是无法被压缩的,具体原因不清楚,比如百度的ueditor,my97等
css里面会有图片,压缩之后找不到路径,所以不再一个路径下的css要单独压缩,并保证路径一致
例如
bundles.Add(new StyleBundle("~/Content/vendors/My97DatePicker/skin/default/my97Css").Include(
"~/Content/vendors/My97DatePicker/skin/default/datepicker.css"));