栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

基于Asp.Net MVC4 Bundle捆绑压缩技术的介绍

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于Asp.Net MVC4 Bundle捆绑压缩技术的介绍

很高兴,最近项目用到了Asp.Net MVC4 + Entity framework5,发现mvc4加入了Bundle、Web API等技术,着实让我兴奋,以前是用第三方的,这里主要说说Bundle技术。

很多大网站都没有用Bundle技术造成很多资源浪费与性能的牺牲,别小瞧 用上了你会发现他的好处:

将多个请求捆绑为一个请求,减少服务器请求数

 没有使用Bundle技术,debug下看到的是实际的请求数与路径

 

使用Bundle技术,并且拥有缓存功能
调试设置为Release模式并按F5或修改web.config,就可以看到合并与压缩的效果

压缩javascript,css等资源文件,减小网络带宽,提升性能

后台配置

  MVC4在架构上有些变动,简化了原来的Global.asax,增加了一些静态的配置文件在App_Start下面,留意下BundleConfig.cs,顾名思义是Bundle的配置,所有它的配置在这里进行就可以了,当然也可以单独的配置文件。

复制代码 代码如下: public class BundleConfig { // For more information on Bundling, visit http://go.microsoft.com/fwlink/?linkId=254725 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new scriptBundle("~/bundles/jquery").Include( "~/scripts/jquery-{version}.js")); bundles.Add(new scriptBundle("~/bundles/jqueryui").Include( "~/scripts/jquery-ui-{version}.js")); bundles.Add(new scriptBundle("~/bundles/jqueryval").Include( "~/scripts/jquery.unobtrusive*", "~/scripts/jquery.validate*")); // Use the development version of Modernizr to develop with and learn from. Then, when you're // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. bundles.Add(new scriptBundle("~/bundles/modernizr").Include( "~/scripts/modernizr-*")); bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); bundles.Add(new StyleBundle("~/Content/themes/base/css").Include( "~/Content/themes/base/jquery.ui.core.css", "~/Content/themes/base/jquery.ui.resizable.css", "~/Content/themes/base/jquery.ui.selectable.css", "~/Content/themes/base/jquery.ui.accordion.css", "~/Content/themes/base/jquery.ui.autocomplete.css", "~/Content/themes/base/jquery.ui.button.css", "~/Content/themes/base/jquery.ui.dialog.css", "~/Content/themes/base/jquery.ui.slider.css", "~/Content/themes/base/jquery.ui.tabs.css", "~/Content/themes/base/jquery.ui.datepicker.css", "~/Content/themes/base/jquery.ui.progressbar.css", "~/Content/themes/base/jquery.ui.theme.css")); } }

这里大家可以按模块化去配置,我们看到的下面的Url对应的就是上面的bundles.Add(...) 所增加的js、css的virtualPath

需要注意的是不同virtualPath 增加的相同的资源文件,会被重复加载!

前台调用

 对于公共的资源文件,通常我们都会放到_Layout.cshtml (webform中的母板页) 文件中

   script文件引用:@scripts.Render(virtualPath[,virtualPath1][,virtualPath2][,...])
   CSS文件引用:  @Styles.Render(virtualPath[,virtualPath1][,virtualPath2][,...])

复制代码 代码如下: @Styles.Render("~/Content/css") @Styles.Render("~/Content/themes/base/css") ...@scripts.Render("~/bundles/jquery") @scripts.Render("~/bundles/jqueryui") @RenderSection("scripts", required: false)

正则匹配需要的,过滤不需要的

复制代码 代码如下: bundles.IgnoreList.Clear(); bundles.IgnoreList.Ignore("*.debug.js"); bundles.IgnoreList.Ignore("*.min.js"); bundles.IgnoreList.Ignore("*-vsdoc.js"); bundles.IgnoreList.Ignore("*intellisense.js"); bundles.Add(new scriptBundle("~/bundles/jquery", jqueryCdn).Include( "~/scripts/jquery-{version}.js")); //匹配jquery版本    bundles.Add(new scriptBundle("~/bundles/jqueryval").Include( "~/scripts/jquery.unobtrusive*", //匹配文件名前缀为jquery.unobtrusive "~/scripts/jquery.validate*")); ...

使用CDN

复制代码 代码如下: bundles.UseCdn = true; //使用CDN string jqueryCdn = "http:deom.jb51.net/jslib/jquery/jquery-1.7.1.min.js"; bundles.Add(new scriptBundle("~/bundles/jquery", jqueryCdn).Include( "~/scripts/jquery-{version}.js"));

当cdn服务器挂了或不能访问了,这里就会选择本地的资源文件,debug下mvc 会让我们看到他原来的面具,这点非常好利于我们调试。  
   

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/57828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号