惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
小众软件
小众软件
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
MyScale Blog
MyScale Blog

博客园 - 齐世昌

.resources文件转.resx 文件 同一Session中的aspx页面的并发限制 Diving Into Lync Client Logins Passing JavaScript Objects to Managed Code UI抑制限制(UI Suppression Limitations) JSON对象末尾多余逗号问题 T430 Windows 8 的USB3.0无法识别 【翻译】数据库上的IO已冻结,不需要任何用户操作 禁用笔记本WiFi热点网络连接问题 推荐一个在线压缩JavaScript和CSS的网站 Windows 8不支持Windows XP 模式 又是一年(2012年) 本地测试网址推荐 text-overflow: ellipsis 在IE8、9下显示问题 Windows Server 2012 不支持SharePoint Server 2010(KB2724471) VirtualPathUtility.IsAppRelative的bug? 神奇的img 受信任站点在Windows 7 和Windows XP的注册表中的不同 contenteditable属性浏览器支持情况(基本支持)-工作草稿
【翻译】为Web Forms添加捆绑(Bundling)和压缩(Minificati...
齐世昌 · 2013-05-05 · via 博客园 - 齐世昌

-- Adding Bundling and Minification to Web Forms

创建新.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类中。以下是类的完整代码:

using System.Web.Optimization;

public class BundleConfig
{
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*"));

bundles.Add(new ScriptBundle("~/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(new ScriptBundle("~/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(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
}
}

注册捆绑(Register the Bundle)

模板创建代码在Global.asax文件中的Application-Start方法中挂接捆绑注册。

void Application_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterOpenAuth();
}

引用捆绑(Reference the Bundles)

添加jQuery捆绑到<asp:PlaceHolder >标记如下代码所示:

<asp:PlaceHolder runat="server">
<%: Scripts.Render("~/bundles/modernizr") %>
<%: Scripts.Render("~/bundles/jquery") %>
<%: Scripts.Render("~/bundles/jqueryui") %>
</asp:PlaceHolder>

注释掉ScriptManager标签中的jQuery脚本引用如下所示:

<body>
<form runat="server">
<asp:ScriptManager runat="server">
<Scripts>
<%--
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
--
%>
</Scripts>
</asp:ScriptManager>
<header>

CSS捆绑(CSS Bundles)

检查文件Bundle.config——包含用来创建CSS样式绑定的标记。

<?xml version="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")
%>