在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. Feature Pyramid Networks for Object Detection比较FPN、UNet、Conv-Deconv

    https://vitalab.github.io/deep-learning/2017/04/04/feature-pyramid-network.html Feature Pyramid Netw ...

  2. Virtual Box虚拟机安装Ubuntu16.04以及整理的一些基本操作

    事先声明,参考自:https://www.cnblogs.com/wyt007/p/9856290.html 撰写此文,纯属是为了便利以后换电脑重装. 转载请注明地址:https://www.cnbl ...

  3. java json转换(二)

    package com.kps.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArra ...

  4. hdu 5536 Chip Factory (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...

  5. Thinkphp5.1 ORM UML

    Thinkphp5.1 ORM  UML think-orm

  6. 【BZOJ5495】[十二省联考2019]异或粽子(主席树,贪心)

    [BZOJ5495][十二省联考2019]异或粽子(主席树,贪心) 题面 BZOJ 洛谷 题解 这不是送分题吗... 转异或前缀和,构建可持久化\(Trie\). 然后拿一个堆维护每次的最大值,每次如 ...

  7. LoadRunner开发ftp协议接口之上传文件脚本

    Action() { //建立一个ftp对象 FTP ftp1=0; //建立FTP连接并登录 ftp_logon_ex(&ftp1,"ftpLogon", "U ...

  8. 【系统架构】软件核心复杂性应对之道-领域驱动DDD(Domain-Driven Design)

    前言 领域驱动设计是一个开放的设计方法体系,目的是对软件所涉及到的领域进行建模,以应对系统规模过大时引起的软件复杂性的问题,本文将介绍领域驱动的相关概念. 一.软件复杂度的根源 1.业务复杂度(软件的 ...

  9. python之路day04--列表的增删改查,嵌套、元组的嵌套、range、for循环嵌套

    列表增删改查 增加 append li = ['taibai','zy','nvshen'] li.append('aa') print(li) #['taibai', 'zy', 'nvshen', ...

  10. linux c编程之fcntl

    fcntl可实现对指定文件描述符的各种操作,其函数原型如下: int fcntl(int fd, int cmd, ... /* arg */ ); 其中,操作类型由cmd决定.cmd可取如下值: F ...