[普通]ASP.Net WebForms使用JS,CSS合并压缩技术

作者(passion) 阅读(1690次) 评论(0) 分类( ASP.NET)
想必大家都早早安装VS2012尝鲜了吧?其实我也是,只是最近才腾出时间来好好研究研究,看看微软新的项目类型架构有什么变化。
    今天就说这个Bundle.config。
    想必做网站的都了解过网站优化吧,那么了解过网站优化就应该知道最起码的JS,CSS压缩合并技术吧,哈哈,这里就说到Bundle.config引入的目的和意义了。
   新建一个ASP.Net Web Forms Application,使用.NET4.5框架。如下图:

 
运行应用程序,浏览器打开默认起始页,在IE9浏览器下按F12,调出界面代码调试工具,切换到Script标签下,可看到项目目录Script文件夹下的JS引用文件,如下图
 

 
这些浏览器请求自服务器的JS代码就是经过压缩合并过的。
   如何使用呢?
   在.NET4.5框架的webfroms应用程序下,默认模板下还添加了一个App_Start文件夹,里面一个是BundleConfig.cs文件,一个是AuthConfig.cs文件,第一个就是我们用来动态加载配置需要做压缩引用的JS或者CSS文件的处理程序类,第二个是第三方信息引入认证处理程序类,其实就是OAuth。当然我们要说的是第一个。

创建js合并包

在App_Start下的BundleConfig 类中添加jQuery, jQuery.UI and jQuery validation js,完整的示例代码如下:


01.using System.Web.Optimization;
02. 
03.public class BundleConfig
04.{
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。

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>
« 上一篇:wifi共享上网(至尊版wifi)
« 下一篇:ASP.NET附加数据库文件的方式,如何发布到IIS7而不导致SQLServer出错
在这里写下您精彩的评论
  • 微信

  • QQ

  • 支付宝

返回首页
返回首页 img
返回顶部~
返回顶部 img