http://www.cnblogs.com/wuhuacong/p/4073203.html

在Web开发的时候,我们很多时候,需要引用很多CSS文件、JS文件,随着使用更多的插件或者独立样式文件,可能我们的Web界面代码会越来越臃肿,看起来也很累赘,在MVC里面提供了一个Bundle的对象,用来简化页面代码非常方便,本文主要介绍在我的MVC框架里面,如何使用bundles来简化页面的代码的。

1、常规的页面代码

我们知道,随着使用更多的一些效果,我们可能不断引入一些新的JS和CSS文件,已达到Web界面更好的表现效果。这样也就逐步增加了文件代码的行数,造成相对比较臃肿的场景,如下面的我正常使用的Web界面,头部需要引入很多JS和CSS文件。

    @*添加Jquery EasyUI的样式*@<link href="~/Content/JqueryEasyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" /><link href="~/Content/JqueryEasyUI/themes/icon.css" rel="stylesheet" type="text/css" /><link href="~/Content/themes/Default/style.css" rel="stylesheet" type="text/css" /><link href="~/Content/themes/Default/default.css" rel="stylesheet" type="text/css" />@*添加Jquery,EasyUI和easyUI的语言包的JS文件*@<script type="text/javascript" src="~/Content/JqueryEasyUI/jquery.min.js"></script><script type="text/javascript" src="~/Content/JqueryEasyUI/jquery.easyui.min.js"></script><script type="text/javascript" src="~/Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js"></script>@*日期格式的引用*@<script src="~/Content/datapattern.js"></script><!--引用EasyUI扩展--><link href="~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/easyui.extend.css" rel="stylesheet" /><link href="~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/icon.css" rel="stylesheet" /><script src="~/Content/JQueryTools/jQuery.easyui-extend/jquery.easyui.extend.min.js"></script>@*引用提示控件*@<link rel="stylesheet" type="text/css" href="~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.css" media="screen" /><script type="text/javascript" src="~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.js"></script>@*常用的一些组件业务脚本函数*@<script type="text/javascript" src="~/Scripts/ComponentUtil.js"></script>

然后这样的文件总是在不断的复制做,非常不雅观,维护也不方便。

在ASP.NET MVC出来之后,引入了一个叫做Bundle的东西,它用来将js和css文件捆绑为一个块进行输出,能够极大简化界面代码,并默认对这些内容进行压缩处理,提高效率。

最终简化的界面代码如下所示。

    @using System.Web.Optimization;@Scripts.Render("~/bundles/jquery")@Styles.Render("~/Content/css")@Scripts.Render("~/bundles/jquerytools")@Styles.Render("~/Content/jquerytools")

2、使用bundles优化的界面操作

为了实现上面的效果,我们需要进行几步的操作处理。

在App_Start里面的BundleConfig里面增加几行处理代码,如下所示。

    public class BundleConfig{// 有关 Bundling 的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=254725public static void RegisterBundles(BundleCollection bundles){//为了减少太多的Bundles命名,定义的CSS的Bundle为:"~/Content/css"、"~/Content/jquerytools"//定义的Script的Bundles为:"~/bundles/jquery"、"~/bundles/jquerytools"//Jquery必备的StyleBundle和ScriptBundleStyleBundle css = new StyleBundle("~/Content/css");ScriptBundle jquery = new ScriptBundle("~/bundles/jquery");//添加Jquery EasyUI的样式css.Include("~/Content/JqueryEasyUI/themes/default/easyui.css", "~/Content/JqueryEasyUI/themes/icon.css","~/Content/themes/Default/style.css","~/Content/themes/Default/default.css");//添加Jquery,EasyUI和easyUI的语言包的JS文件,日期格式的引用jquery.Include("~/Content/JqueryEasyUI/jquery.min.js","~/Content/JqueryEasyUI/jquery.easyui.min.js","~/Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js","~/Content/datapattern.js");//常用的一些组件业务脚本函数(建议放到最后)jquery.Include("~/Scripts/ComponentUtil.js");//扩展的StyleBundle和ScriptBundleStyleBundle cssExtend = new StyleBundle("~/Content/jquerytools");ScriptBundle jqueryExtend = new ScriptBundle("~/bundles/jquerytools");//引用EasyUI扩展cssExtend.Include("~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/easyui.extend.css","~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/icon.css");jqueryExtend.Include("~/Content/JQueryTools/jQuery.easyui-extend/jquery.easyui.extend.min.js");//引用消息提示控件cssExtend.Include("~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.css");jqueryExtend.Include("~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.js");//其他一些辅助脚本和样式//全部增加到集合里面去bundles.Add(css);bundles.Add(jquery);bundles.Add(cssExtend);bundles.Add(jqueryExtend);}}

上面代码,我们增加一些必要的Jquery和一些扩展给的JqueryTool的脚本和样式,方便统一化管理。

默认的情况下,Bundle是按照字母顺序进行排序的,如果需要按照增加的次序进行排序,这需要写一个自定义的排序规则进行处理,如下所示

    /// <summary>/// 自定义Bundles排序/// </summary>internal class AsIsBundleOrderer : IBundleOrderer{public virtual IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files){return files;}}

然后在调用的时候,修改对象的排序规则即可。

            ScriptBundle jqueryExtend = new ScriptBundle("~/bundles/jquerytools");jqueryExtend.Orderer = new AsIsBundleOrderer();

接着在Global.asa.cs里面,增加对Bundle的注册,如下所示。

        protected void Application_Start(){AreaRegistration.RegisterAllAreas();BundleConfig.RegisterBundles(BundleTable.Bundles);WebApiConfig.Register(GlobalConfiguration.Configuration);FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);}

最后在MVC的视图里面,就可以使用Bundle来简化界面代码了。简化后的界面代码如下所示。

<!DOCTYPE html>
<html><head><title>用户管理</title><meta name="viewport" content="width=device-width" />@using System.Web.Optimization;@Scripts.Render("~/bundles/jquery")@Styles.Render("~/Content/css")@Scripts.Render("~/bundles/jquerytools")@Styles.Render("~/Content/jquerytools")...............

运行界面,虽然使用了简化版本的代码,依旧正常运行

页面代码输出则还是和原先未优化的一致。

<!DOCTYPE html>
<html><head><title>用户管理</title><meta name="viewport" content="width=device-width" /><script src="/Content/JqueryEasyUI/jquery.min.js"></script><script src="/Content/JqueryEasyUI/jquery.easyui.min.js"></script><script src="/Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js"></script><script src="/Content/datapattern.js"></script><script src="/Scripts/ComponentUtil.js"></script><link href="/Content/JqueryEasyUI/themes/default/easyui.css" rel="stylesheet"/><link href="/Content/JqueryEasyUI/themes/icon.css" rel="stylesheet"/><link href="/Content/themes/Default/style.css" rel="stylesheet"/><link href="/Content/themes/Default/default.css" rel="stylesheet"/><script src="/Content/JQueryTools/jQuery.easyui-extend/jquery.easyui.extend.min.js"></script><script src="/Content/JQueryTools/jNotify/jquery/jNotify.jquery.js"></script><link href="/Content/JQueryTools/jQuery.easyui-extend/extend/themes/easyui.extend.css" rel="stylesheet"/><link href="/Content/JQueryTools/jQuery.easyui-extend/extend/themes/icon.css" rel="stylesheet"/><link href="/Content/JQueryTools/jNotify/jquery/jNotify.jquery.css" rel="stylesheet"/>

转载于:https://www.cnblogs.com/telwanggs/p/7125928.html

(转)基于MVC4+EasyUI的Web开发框架经验总结(11)--使用Bundles处理简化页面代码...相关推荐

  1. 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...

  2. [转载]基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...

  3. 基于MVC4+EasyUI的Web开发框架经验总结(6)--在页面中应用下拉列表的处理

    在很多Web界面中,我们都可以看到很多下拉列表的元素,有些是固定的,有些是动态的:有些是字典内容,有些是其他表里面的名称字段:有时候引用的是外键ID,有时候引用的是名称文本内容:正确快速使用下拉列表的 ...

  4. (转)基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    http://www.cnblogs.com/wuhuacong/p/3871991.html 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览 在博客园很多文章 ...

  5. 基于MVC4+EasyUI的Web开发框架经验总结(11)--使用Bundles处理简化页面代码

    基于MVC4+EasyUI的Web开发框架经验总结(11)--使用Bundles处理简化页面代码 在Web开发的时候,我们很多时候,需要引用很多CSS文件.JS文件,随着使用更多的插件或者独立样式文件 ...

  6. (转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式...

    http://www.cnblogs.com/wuhuacong/p/4085682.html 在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交, ...

  7. (转)基于MVC4+EasyUI的Web开发框架经验总结(10)--在Web界面上实现数据的导入和导出...

    http://www.cnblogs.com/wuhuacong/p/3873498.html 数据的导入导出,在很多系统里面都比较常见,这个导入导出的操作,在Winform里面比较容易实现,我曾经在 ...

  8. (转)基于MVC4+EasyUI的Web开发框架经验总结(7)--实现省份、城市、行政区三者联动...

    http://www.cnblogs.com/wuhuacong/p/3841338.html 为了提高客户体验和进行一些技术探索,现在正准备把我自己的客户关系管理系统CRM在做一个Web的版本,因此 ...

  9. (转)基于MVC4+EasyUI的Web开发框架经验总结(1)-利用jQuery Tags Input 插件显示选择记录...

    http://www.cnblogs.com/wuhuacong/p/3667703.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...

最新文章

  1. DOS批处理的字符串功能
  2. leetcode-402 移掉K位数组
  3. 【JSP/Servlet】getParameter和getAttribute的使用、区别
  4. 深入进货单-价格跟踪----宇然电脑公司管理软件
  5. UI设计干货素材|动效导航,漂亮的悬停动效
  6. button 样式_Tkinter (3)---Button
  7. 在Master page 里 CompositeControl 的事件失效了
  8. splice方法_Array中splice用法
  9. 腾讯魔镜壁纸所在位置
  10. 安卓课设:翻译君APP(附开源地址)
  11. 战神引擎清理数据教程[保留跟不保留账号
  12. php 如何实现 访问不带后缀名
  13. 拜腾“难”飞:全球化饮鸩止渴?
  14. 如何安装微信解决小黑块问题(一定可以使用)
  15. iPhone出现白苹果怎么修复?简单3步即可解决
  16. 如何在卸载Citrix Receiver之后清理删除残留文件 【Mac】
  17. 学生个人单页面网页作业 学生网页设计成品 静态HTML网页单页制作 dreamweaver网页设计与制作代码 web前端期末大作业
  18. 统一语言为什么不能统一(一)
  19. Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain:
  20. 手机+固定电话+传真的正则

热门文章

  1. string 释放_由String,String Builder,String Buffer 引起的面试惨案
  2. (20)Xilinx FPGA型号(FPGA不积跬步101)
  3. (08)FPGA时钟概念
  4. 生成top图_最小生成树(Kruskal和Prim算法)
  5. telnet黑屏就是通_黑屏的智能农贸市场竟通过验收?市场监管部门:工作力度不够 | 电视问政...
  6. linux下的网桥介绍
  7. arm-2009q1-203-arm-none-linux-gnueabi.bin安装方法
  8. SVD 与 PCA 的直观解释(2): 特征值与特征向量
  9. java简单功能测试,java 自动化测试小功能集锦
  10. 红帽linux安装显卡驱动,RedHat Enterprise Linux 6 安装ATI显卡驱动