ASP.NET Web Forms 4.5的新特性(一):強型別資料控制項和Bundling

來源:互聯網
上載者:User

在微軟線上發布了Visual Studio 2012後,我們也能清晰地看到ASP.NET Web Forms 4.5中的一些新特性了。

今天先看兩個新特性:強型別資料控制項和Bundling。

強型別資料控制項

在出現強型別資料控制項前,我們綁定資料控制項時,前台一般使用Eval或者DataBinder.Eval(Container.DataItem,"FieldName")的形式。

1 <%# DataBinder.Eval(Container.DataItem,"FieldName") %>
2 <%# Eval("FieldName") %>

而在ASP.NET Web Forms 4.5中出現了強型別資料控制項,可以後台綁定資料的控制項多了個屬性:ItemType。

當指定了控制項的ItemType後就可以在前台使用強型別綁定資料了。

指定ItemType。

使用強型別綁定資料。

屆時園子裡估計又要掀起一番爭論ORM的風潮

Bundling

不怎麼好翻譯,類似於資源檔的壓縮包,索性不翻譯它了。

我們知道Web效能最佳化的一個主要的方法就是減少HTTP請求,詳細可見YSlow裡面的Minimize HTTP Requests。

由此才出現了css sprite、壓縮CSS/JS的工具,目的都是為了盡量減少HTTP的請求。

之前.NET架構下會有很多的第三方架構,微軟也出過一個Microsoft Ajax Minifier,我也曾經也過一篇文章介紹過。

而這次在ASP.NET Web Forms 4.5中,索性直接添加了此功能,多了一個叫做Bundle的類。

 1 public class BundleConfig
 2     {
 3         // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254726
 4         public static void RegisterBundles(BundleCollection bundles)
 5         {
 6             bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
 7                   "~/Scripts/WebForms/WebForms.js",
 8                   "~/Scripts/WebForms/WebUIValidation.js",
 9                   "~/Scripts/WebForms/MenuStandards.js",
10                   "~/Scripts/WebForms/Focus.js",
11                   "~/Scripts/WebForms/GridView.js",
12                   "~/Scripts/WebForms/DetailsView.js",
13                   "~/Scripts/WebForms/TreeView.js",
14                   "~/Scripts/WebForms/WebParts.js"));
15 
16             bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
17                     "~/Scripts/jquery-{version}.js"));
18 
19             bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(
20                 "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
21                 "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
22                 "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
23                 "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));
24 
25             bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
26                   "~/Content/themes/base/jquery.ui.core.css",
27                   "~/Content/themes/base/jquery.ui.resizable.css",
28                   "~/Content/themes/base/jquery.ui.selectable.css",
29                   "~/Content/themes/base/jquery.ui.accordion.css",
30                   "~/Content/themes/base/jquery.ui.autocomplete.css",
31                   "~/Content/themes/base/jquery.ui.button.css",
32                   "~/Content/themes/base/jquery.ui.dialog.css",
33                   "~/Content/themes/base/jquery.ui.slider.css",
34                   "~/Content/themes/base/jquery.ui.tabs.css",
35                   "~/Content/themes/base/jquery.ui.datepicker.css",
36                   "~/Content/themes/base/jquery.ui.progressbar.css",
37                   "~/Content/themes/base/jquery.ui.theme.css"));
38         }
39     }

Bundle還支援將CSS壓縮的檔案配置寫在config檔案裡,使得維護更加方便,同時也支援CDN和檔案萬用字元等。

可以通過在Application_Start裡註冊配置。

1 void Application_Start(object sender, EventArgs e)
2 {
3     BundleConfig.RegisterBundles(BundleTable.Bundles);
4 }

使用。

1 <%: Scripts.Render("~/bundles/modernizr") %>
2 <%: Scripts.Render("~/bundles/jquery") %>
3 <%: Scripts.Render("~/bundles/jqueryui") %>

壓縮前後請求數對比(圖片來源)。

壓縮合并前。

壓縮後。

這樣,當此屬性成為了ASP.NET內建的一個功能後,我們只需要很簡單地配置就可以實現最佳化準則裡面的“減少掉HTTP請求數”這一條了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.