在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"));

期货大赛项目|十,MVC对js和css的压缩的更多相关文章

  1. 期货大赛项目|四,MVC的数据验证

    上图先看下效果 样式先不说,先了解下数据验证是怎么实现的 一 必须是强类型的视图 二 这些显示提示的话语,都在强类型的实体中 三 必须使用Html.BeginForm或者Html.AjaxBeginF ...

  2. 【MVC】 js,css 压缩

    [MVC] js,css 压缩 一. 引用 System.Web.Optimization.dll : 使用 Nuget ,在控制台输入 Install-Package Microsoft.AspNe ...

  3. js、css动态压缩页面代码

    1.js.css动态压缩页面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  4. 期货大赛项目|五,表格插件datatatables在MVC中的应用

    系统中都需要表格,我见过最好的表格就是Datatables了,但中文文档有限,英文能力有限,就写一些简单用法 上图看效果先 要了分页和排序 基本用法 引入js和css bundles.Add(new ...

  5. 期货大赛项目|八,ueditor的应用

    百度开发的富文本编辑器还是很不错的,可以已经不维护了 下载ueditor1_4_3_3-utf8-net放到项目中 找到net文件夹下config.json 修改以下两行 "imageUrl ...

  6. 期货大赛项目|六,iCheck漂亮的复选框

    废话不多说,直接上图 对,还是上篇文章的图,这次我们不研究datatables,而是看这个复选框,比平常的复选框漂亮太多 看看我是如何实现的吧 插件叫iCheck 用法也简单 引入js和css $(& ...

  7. 期货大赛项目|九,fileinput插件的应用

    引入JS和CSS bundles.Add(new ScriptBundle("~/bundles/fileinputJs").Include( "~/Content/ve ...

  8. node作为前台的项目如何打包静态js和css并生成版本号,loader插件的使用

    一.使用场景: 1.node创建的前台项目需要输入地址展示页面 2.有设置缓存或者cdn的需要在静态文件更改时能使用新的而不是缓存的,需要版本号这里 3.可能需要压缩静态文件的 二.一些参考地址,需要 ...

  9. 关于ASP.NET MVC的js和css资源管理

    本文来源于博客园-钱智慧,转载请注明出处 通过这篇文章和这个回答,我们总结如下: 浏览器针对一个域名,最多只会开启6个线程来加载文件,比如head中如果有7个引入文件(js文件或者css文件)的标签, ...

随机推荐

  1. 线程池、进程池(concurrent.futures模块)和协程

    一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...

  2. kubernetes-kubeadm自动生成的证书过期的解决方法

    拉取kubernetes的源码: git clone https://github.com/kubernetes/kubernetes.git 切换版本: cd kubernetes &&am ...

  3. 关于data()获取不到得原因

    ..原因很简单,版本高低问题  从jQuery 1.4.3起, HTML 5 data- 属性 将自动被引用到jQuery的数据对象中.  所以,还是尽量保持用attr来获取自定义属性

  4. Lua语法基础(二)

    1. 函数 1.1 函数声明 默认为全局 局部函数使用local关键字声明 1.2 参数 ...等同于Python中*args三个点表示可变参数 1.3 获取参数长度的两种方式 (1)将传入的参数.. ...

  5. epoll ET(边缘触发) LT(水平触发)

    EPOLL事件有两种模型: Edge Triggered (ET) 边缘触发只有数据到来,才触发,不管缓存区中是否还有数据.Level Triggered (LT) 水平触发只要有数据都会触发. 首先 ...

  6. CMDB服务器管理系统【s5day91】:数据库表结构补充

    1.表机构补充图 2.用户信息表(UserProfile) 1.解决了什么问题 1.这台服务器是谁管理的?2.真正出问题了我敢上去改代码了?不能3.所以一台机器必须有运维人员和业务负责人,但是业务负责 ...

  7. SEO学习知识

    监控流量的工具 百度统计 CNZZ  51LA 谷歌分析工具 如何从平台借流量? 竞价(付费).SEO 关键词定位: 定位人:负责人 将公司的业务全部列出来 选词: 根据定位的关键词选择出我们需要优化 ...

  8. HDU 5963(游戏 博弈+规律)

    题意是: 一群男生和一群女生玩游戏:给出一棵 n 个节点的树,这棵树的每条边有一个权值 0 或 1. 在一局游戏开始时,确定一个节点作为根.从女生开始,双方轮流进行操作. 当一方操作时,要先选择一个不 ...

  9. 关于java集合的练习

    关于java集合的练习 练习一:Collection集合练习 一.产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台. public cl ...

  10. 选择性搜索(SS)算法

    一.目标检测和目标识别 目标识别(object recognition)是要指明一张图像中包含哪类目标.输入是图像,输出是图像中的目标属于的类别(class probability).目标检测是识别出 ...