

在App_Start下的BundleConfig 类中添加jQuery, jQuery.UI and jQuery validation js,完整的示例代码如下:
01.using System.Web.Optimization;02. 03.public class BundleConfig04.{05.public static void RegisterBundles(BundleCollection bundles)06.{07.bundles.Add(new ScriptBundle("~/bundles/jquery").Include(08."~/Scripts/jquery-{version}.js"));09. 10.bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(11."~/Scripts/jquery-ui-{version}.js"));12. 13.bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(14."~/Scripts/jquery.unobtrusive*",15."~/Scripts/jquery.validate*"));16. 17.bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(18."~/Scripts/WebForms/WebForms.js",19."~/Scripts/WebForms/WebUIValidation.js",20."~/Scripts/WebForms/MenuStandards.js",21."~/Scripts/WebForms/Focus.js",22."~/Scripts/WebForms/GridView.js",23."~/Scripts/WebForms/DetailsView.js",24."~/Scripts/WebForms/TreeView.js",25."~/Scripts/WebForms/WebParts.js"));26. 27.bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(28."~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",29."~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",30."~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",31."~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));32. 33.bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(34."~/Scripts/modernizr-*"));35.}36.}下面的代码举例如何在项目Global.asax的Application_Start方法中注册已经预定义的压缩包。
1.void Application_Start(object sender, EventArgs e)2.{3.BundleConfig.RegisterBundles(BundleTable.Bundles);4.AuthConfig.RegisterOpenAuth();5.}在页面使用<asp:PlaceHolder> 标记,来引入页面需要引用的包,示例代码如下:
1.<asp:PlaceHolder runat="server"> 2.<%: Scripts.Render("~/bundles/modernizr") %>3.<%: Scripts.Render("~/bundles/jquery") %>4.<%: Scripts.Render("~/bundles/jqueryui") %>5.</asp:PlaceHolder>然后还需要将使用ScriptManager标签引用的代码注释掉:
01.<body>02.<form runat="server">03.<asp:ScriptManager runat="server">04.<Scripts>05.<%--06.<asp:ScriptReference Name="MsAjaxBundle" />07.<asp:ScriptReference Name="jquery" />08.<asp:ScriptReference Name="jquery.ui.combined" />09.<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />10.<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web"Path="~/Scripts/WebForms/WebUIValidation.js" />11.<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />12.<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />13.<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />14.<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />15.<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />16.<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />17.<asp:ScriptReference Name="WebFormsBundle" />18.--%>19.</Scripts>20.</asp:ScriptManager>21.<header>上面引用的html5的标记,关键字。
至此需要压缩打包的JS工作做完了,接下来搞定CSS。
打开Bundle.config文件,里面有css打包的示例配置方式:
01.<?xml version="1.0" encoding="utf-8" ?>02.<bundles version="1.0">03.<styleBundle path="~/Content/css">04.<include path="~/Content/Site.css" />05.</styleBundle>06.<styleBundle path="~/Content/themes/base/css">07.<include path="~/Content/themes/base/jquery.ui.core.css" />08.<include path="~/Content/themes/base/jquery.ui.resizable.css" />09.<include path="~/Content/themes/base/jquery.ui.selectable.css" />10.<include path="~/Content/themes/base/jquery.ui.accordion.css" />11.<include path="~/Content/themes/base/jquery.ui.autocomplete.css" />12.<include path="~/Content/themes/base/jquery.ui.button.css" />13.<include path="~/Content/themes/base/jquery.ui.dialog.css" />14.<include path="~/Content/themes/base/jquery.ui.slider.css" />15.<include path="~/Content/themes/base/jquery.ui.tabs.css" />16.<include path="~/Content/themes/base/jquery.ui.datepicker.css" />17.<include path="~/Content/themes/base/jquery.ui.progressbar.css" />18.<include path="~/Content/themes/base/jquery.ui.theme.css" />19.</styleBundle>20.</bundles>你可以添加自己需要压缩合并的CSS文件在这个配置文件里面。
下面的代码示例CSS包和JS包的界面引用方式,如果你愿意,一个方法内是可以引用多个包的:www.it165.net
1.<%: Styles.Render("~/Content/themes/base/css",2."~/Content/css") %>3.<%: Scripts.Render("~/bundles/modernizr") %>4.<%: Scripts.Render("~/bundles/jquery",5."~/bundles/jqueryui") %>PS:据我理解也可以使用如下标记来引用绑定的CSS文件包
1.<webopt:BundleReference runat="server" Path="~/Content/css" />完整代码示例如下:
01.<!DOCTYPE html>02.<html lang="en">03.<head runat="server">04.<meta charset="utf-8" />05.<title><%: Page.Title %> - My <a href="http://www.it165.net/pro/webasp/" target="_blank" class="keylink">ASP</a>.NET Application</title>06.<asp:PlaceHolder runat="server"> 07.<%: Scripts.Render("~/bundles/modernizr") %>08.</asp:PlaceHolder> 09.<webopt:BundleReference runat="server" Path="~/Content/css" />10.<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />11.<meta name="viewport" content="width=device-width" />12.</head>13.<body>14.<form runat="server">15.<asp:ScriptManager runat="server">16.<Scripts>17.<%--18.<asp:ScriptReference Name="MsAjaxBundle" />19.<asp:ScriptReference Name="jquery" />20.<asp:ScriptReference Name="jquery.ui.combined" />21.<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />22.<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web"Path="~/Scripts/WebForms/WebUIValidation.js" />23.<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />24.<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />25.<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />26.<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />27.<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />28.<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />29.<asp:ScriptReference Name="WebFormsBundle" />30.Scripts--%>31.</Scripts>32.</asp:ScriptManager>33.<header>34.<div class="content-wrapper">35.<div class="float-left">36.<p class="site-title">37.<a runat="server" href="~/">your logo here</a>38.</p>39.</div>40.<div class="float-right">41.<section id="login">42.</section>43.<nav>44.<ul id="menu">45.<li><a runat="server" href="~/">Home</a></li>46.</ul>47.</nav>48.</div>49.</div>50.</header>51.<div id="body">52.<section class="content-wrapper main-content clear-fix">53.</section>54.</div>55.<footer>56.<div class="content-wrapper">57.</div>58.</footer>59.</form>60.</body>61.</html>
微信
支付宝