为Web Forms添加捆绑(Bundling)和压缩(Minification)
来源:互联网 发布:程序员高薪 编辑:程序博客网 时间:2024/04/30 04:55
创建新.Net 4.5 framework的ASP.NET Web Forms应用程序
运行应用程序,启动IE F-12开发人员工具。选择脚步标签,然后选择资产(assets)按钮来浏览JavaScript文件。
你可以在左侧面板选择一个JavaScript文件。 注意使用的是完整版本的文件(非压缩版本)
创建jQuery捆绑(Creating the jQuery Bundles)
添加jQuery,jQuery.UI和jQuery验证到App_Start文件夹的BundleConfig类中。以下是类的完整代码:
usingSystem.Web.Optimization;
public classBundleConfig
{
public static void RegisterBundles(BundleCollectionbundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(newScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(newScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
bundles.Add(newScriptBundle("~/bundles/WebFormsJs").Include(
"~/Scripts/WebForms/WebForms.js",
"~/Scripts/WebForms/WebUIValidation.js",
"~/Scripts/WebForms/MenuStandards.js",
"~/Scripts/WebForms/Focus.js",
"~/Scripts/WebForms/GridView.js",
"~/Scripts/WebForms/DetailsView.js",
"~/Scripts/WebForms/TreeView.js",
"~/Scripts/WebForms/WebParts.js"));
bundles.Add(newScriptBundle("~/bundles/MsAjaxJs").Include(
"~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
"~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
"~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
"~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));
bundles.Add(newScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
}
}
注册捆绑(Register the Bundle)
模板创建代码在Global.asax文件中的Application-Start方法中挂接捆绑注册。
voidApplication_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterOpenAuth();
}
引用捆绑(Reference the Bundles)
添加jQuery捆绑到<asp:PlaceHolder >标记如下代码所示:
<asp:PlaceHolderrunat="server">
<%:Scripts.Render("~/bundles/modernizr")%>
<%:Scripts.Render("~/bundles/jquery")%>
<%:Scripts.Render("~/bundles/jqueryui")%>
</asp:PlaceHolder>
注释掉ScriptManager标签中的jQuery脚本引用如下所示:
<body>
<form runat="server">
<asp:ScriptManagerrunat="server">
<Scripts>
<%--
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
--%>
</Scripts>
</asp:ScriptManager>
<header>
CSS捆绑(CSS Bundles)
检查文件Bundle.config——包含用来创建CSS样式绑定的标记。
<?xmlversion="1.0"encoding="utf-8"?>
<bundles version="1.0">
<styleBundle path="~/Content/css">
<include path="~/Content/Site.css"/>
</styleBundle>
<styleBundle path="~/Content/themes/base/css">
<include path="~/Content/themes/base/jquery.ui.core.css"/>
<include path="~/Content/themes/base/jquery.ui.resizable.css"/>
<include path="~/Content/themes/base/jquery.ui.selectable.css"/>
<include path="~/Content/themes/base/jquery.ui.accordion.css"/>
<include path="~/Content/themes/base/jquery.ui.autocomplete.css"/>
<include path="~/Content/themes/base/jquery.ui.button.css"/>
<include path="~/Content/themes/base/jquery.ui.dialog.css"/>
<include path="~/Content/themes/base/jquery.ui.slider.css"/>
<include path="~/Content/themes/base/jquery.ui.tabs.css"/>
<include path="~/Content/themes/base/jquery.ui.datepicker.css"/>
<include path="~/Content/themes/base/jquery.ui.progressbar.css"/>
<include path="~/Content/themes/base/jquery.ui.theme.css"/>
</styleBundle>
</bundles>
你可以添加你自己的样式到文件Bundle.config中。
以下标记显示了CSS捆绑和JavaScript捆绑引用。注意你可以在一个Render方法调用中添加多个绑定。
<%:Styles.Render("~/Content/themes/base/css",
"~/Content/css") %>
<%: Scripts.Render("~/bundles/modernizr")%>
<%: Scripts.Render("~/bundles/jquery",
"~/bundles/jqueryui")%>
- 为Web Forms添加捆绑(Bundling)和压缩(Minification)
- ASP.NET——Bundling and Minification(捆绑和压缩)概述
- asp.net mvc4 使用 System.Web.Optimization 对javascript和style的引入、代码合并和压缩的优化(ScriptBundle,StyleBundle,Bundling and Minification )
- ASP.NET MVC4: Bundling and Minification(打包与压缩)
- Better JavaScript Minification(压缩js)
- 捆绑和缩小(BundleConfig.RegisterBundles) 第二章 : Minification (重点理解)
- 捆绑和缩小(BundleConfig.RegisterBundles) 第二章 : bundling
- 捆绑和缩小(BundleConfig.RegisterBundles) 第六章 : LESS, CoffeeScript, SCSS, Sass Bundling 和注意事项
- MVC4捆绑压缩css和js注意事项
- MVC(BundleConfig) 压缩捆绑技术,减少页面请求,优化客户体验。
- 新捆绑和缩小支持(ASP.NET 4.5系列)
- 为web工程添加struts、hibernate和spring支持(eclipse)
- 从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)
- R12.2 添加managed server(forms、oafm、oacore、c4ws)
- Converting WinForms => Web Forms using CodeDom(转载)
- asp+中文教程(二)---- Asp+ Web Forms
- Web应用程序中的服务器错误 (<authentication mode="Forms">)
- Web Forms
- AS: Unsupported method: AndroidProject.getPluginGeneration().
- 关于explode
- xml与datatable互转
- 安装boost库
- BootStrap入门教程 (二)
- 为Web Forms添加捆绑(Bundling)和压缩(Minification)
- 写给对游戏制作有迷惑的小伙伴
- javax.mail 发送邮件
- 百度MIP计划,移动站的首选
- 切图 移动端自己写好的按钮显示不一样
- caffe:LSTM源码分析
- closesocket函数和WSACleanup函数
- GCD 的几个常见的问题分析
- Struts2服务器端的验证(验证框架)