ASP.NET MVCBundling and minification提高页面加载速度
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.NET MVCBundling and minification提高页面加载速度,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6087字,纯文字阅读大概需要9分钟。
内容图文
![ASP.NET MVCBundling and minification提高页面加载速度](/upload/InfoBanner/zyjiaocheng/608/99f9c0611b8a43309f5f46273e41862a.jpg)
https://www.lanhusoft.com/Article/235.html
Bundling and minification是ASP.NET 4.5中可以用来提高页面加载速度的技术。 它通过减少向服务器请求的数量和请求资源文件大小(css、javascript等等)。 通常浏览器对一个主机host同时发请求都有限制,下表列出了常用的浏览器对于同时发送http请求的限制。
![ASP.NET MVCBundling and minification提高页面加载速度 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430122410466.jpg)
一、在ASP.NET mvc中启用Bundling和minification的效果
我们来看一下一个MVC程序中使用静态资源捆绑打包和压缩前后的变化。 未使用Bundling:![ASP.NET MVCBundling and minification提高页面加载速度 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430122411062.jpg)
![ASP.NET MVCBundling and minification提高页面加载速度 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430122411778.jpg)
- AddAltToImg = function (imageTagAndImageID, imageContext) {
- ///<signature>
- ///<summary> Adds an alt tab to the image
- // </summary>
- //<param name="imgElement" type="String">The image selector.</param>
- //<param name="ContextForImage" type="String">The image context.</param>
- ///</signature>
- var imageElement = $(imageTagAndImageID, imageContext);
- imageElement.attr('alt', imageElement.attr('id').replace(/ID/, ''));
- }
- AddAltToImg = function (n, t) { var i = $(n, t); i.attr("alt", i.attr("id").replace(/ID/, "")) }
二、在ASP.NET MVC中启用Bundling和minification
2.1、ASP.NET MVC中控制Bundling和minification启用方法
ASP.NET MVC中启用Bundling和minification方式有两种方式。 方法1、在Web.Config中compilation结点设置为false。- <system.web>
- <compilation debug="false" />
- <!-- 省略其它代码. -->
- </system.web>
方法2、在代码中加如下面一行代码
- public static void RegisterBundles(BundleCollection bundles)
- {
- bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
- "~/Scripts/jquery-{version}.js"));
- BundleTable.EnableOptimizations = true;
- }
2.2、ASP.NET MVC中使用Bundling和minification方法
打开项目中的App_Start\BundleConfig.cs文件里面有一个方法RegisterBundles就是用来注册要打包和压缩的资源文件。- public static void RegisterBundles(BundleCollection bundles)
- {
- bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
- "~/Scripts/jquery-{version}.js"));
- // 省略其它代码
- BundleTable.EnableOptimizations = true;
- }
在web.config中的debug设置为false,可以在程序中用BundleTable.EnableOptimizations覆盖设置。也就是BundleTable.EnableOptimizations的设置的优先级更高。 上面代码创建了一个名为"~/bundles/jquery"的文件打包,其中包括文件"~/Scripts/jquery-{version}.js",这里的“{version}”是一个占位符。 在视图中引用这个打包资源:
- @Scripts.Render("~/bundles/jquery")
- <script src="/bundles/jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1"></script>
![ASP.NET MVCBundling and minification提高页面加载速度 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430122412015.jpg)
![ASP.NET MVCBundling and minification提高页面加载速度 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430122412120.jpg)
2.3、ASP.NET MVC中Bundling和minification使用CDN
- public static void RegisterBundles(BundleCollection bundles)
- {
- //bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
- // "~/Scripts/jquery-{version}.js"));
- bundles.UseCdn = true; //enable CDN support
- //add link to jquery on the CDN
- var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";
- bundles.Add(new ScriptBundle("~/bundles/jquery",
- jqueryCdnPath).Include(
- "~/Scripts/jquery-{version}.js"));
- // 省略其它代码
- }
- @Scripts.Render("~/bundles/jquery")
- <script type="text/javascript">
- if (typeof jQuery == 'undefined') {
- var e = document.createElement('script');
- e.src = '@Url.Content("~/Scripts/jquery-1.7.1.js")';
- e.type = 'text/javascript';
- document.getElementsByTagName("head")[0].appendChild(e);
- }
- </script>
![ASP.NET MVCBundling and minification提高页面加载速度 - 文章图片](/upload/getfiles/0001/2021/4/30/20210430122412208.jpg)
- 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"));
使用通配符“*”来选择多个文件 考虑到如下情况,当一个文件夹下有很多个js文件,除了像上面一样,在Inculde中全部指定,也可以用通配符“*”来选择多个文件。如项目中有如下文件结构: Scripts\Common\AddAltToImg.js Scripts\Common\ToggleDiv.js Scripts\Common\ToggleImg.js Scripts\Common\Sub1\ToggleLinks.js
调用 | 添加的文件 |
---|---|
Include("~/Scripts/Common/*.js") | AddAltToImg.js, ToggleDiv.js, ToggleImg.js |
Include("~/Scripts/Common/T*.js") | Invalid pattern exception. The wildcard character is only allowed on the prefix or suffix. |
Include("~/Scripts/Common/*og.*") | Invalid pattern exception. Only one wildcard character is allowed. |
"Include("~/Scripts/Common/T*") | ToggleDiv.js, ToggleImg.js |
"Include("~/Scripts/Common/*") | Invalid pattern exception. A pure wildcard segment is not valid. |
IncludeDirectory("~/Scripts/Common", "T*") | ToggleDiv.js, ToggleImg.js |
IncludeDirectory("~/Scripts/Common", "T*",true) | ToggleDiv.js, ToggleImg.js, ToggleLinks.js |
内容总结
以上是互联网集市为您收集整理的ASP.NET MVCBundling and minification提高页面加载速度全部内容,希望文章能够帮你解决ASP.NET MVCBundling and minification提高页面加载速度所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。