ASP.NET MVC Bundle 使用

來源:互聯網
上載者:User

標籤:jquer   服務   focus   合并   bundle   相同   asa   編譯   瀏覽器   

基本使用

Global.asax

protected void Application_Start()        {            BundleConfig.RegisterBundles(BundleTable.Bundles);        }

BundleConfig 

public static void RegisterBundles(BundleCollection bundles) {            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(                        "~/Scripts/jquery-{version}.js"));            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(                      "~/Scripts/bootstrap.js",                      "~/Scripts/respond.js")); } 

視圖

@Scripts.Render("~/bundles/jquery")@Scripts.Render("~/bundles/bootstrap")
啟用Bundle(合并壓縮)的2種方式
  1. 在 BundleConfig 中RegisterBundles方法添加以下代碼

    public static void RegisterBundles(BundleCollection bundles){    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(                 "~/Scripts/jquery-{version}.js"));    // 啟用合并壓縮   BundleTable.EnableOptimizations = true;}
  2. 開啟web.config檔案,設定編譯的debug=false

      <system.web>    <compilation debug="false" targetFramework="4.5" />  </system.web>
變化效果

合并之後減少了請求的數量,相對來說提高了頁面的載入速度

?

注意事項
  • 合并壓縮後的檔案預設緩衝一年。如果你重複開啟網頁,並且配置的檔案也沒有做修改的情況下,伺服器會返回一個 HTTP 304 的狀態代碼,這樣瀏覽器會載入緩衝中的檔案。

  • 圖片路徑問題,將多個CSS合并在一起後,有的時候會導致圖片載入失敗的情況

    解決方案:圖片使用絕對路徑

  • 在 BundleConfig 配置中配置的壓縮的檔案是 a.js (未壓縮版本) ,並且在相同目錄中存在 a.min.js 檔案。當你修改了 a.js 檔案後,希望合并壓縮後的 JS 檔案也包含修改後的內容,然而並非我們所希望的那樣,壓縮後的 JS 檔案還是引用了a.min.js 中的內容,之前修改的並沒有出現在壓縮的 JS 檔案中。同理,CSS也是如此。

引用

ASP.NET MVC4新特性之指令碼壓縮和合并

ASP.NET MVC Bundle 使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.