MVC-採用Bundles方式引入css和js檔案

來源:互聯網
上載者:User

標籤:str   validate   壓縮版   style   end   bsp   路徑   pts   ges   

優點:修改js或css時會自動產生hash版本號碼。

缺點:需要在BundleConfig中先添加對應的檔案,然後在html中再引用對應的bundle,多操作了一步。

web.config中 <compilation debug="true" targetFramework="4.5" />

1、BundleConfig

  如果是偵錯模式,前端頁面會顯示真實的js、css路徑,生產環境是會顯示前面的路徑。在bundles中還可以使用萬用字元引用。

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(                        "~/Scripts/jquery-{version}.js"));            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(                        "~/Scripts/jquery.validate*"));

 

 bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(                      "~/Scripts/bootstrap.js",                      "~/Scripts/respond.js"));            bundles.Add(new StyleBundle("~/Content/css").Include(                      "~/Content/bootstrap.css",                      "~/Content/site.css"));
2、前端頁面
@Styles.Render("~/Content/css")    @Scripts.Render("~/bundles/modernizr")
3、Global檔案中

  要在Application_Start事件中註冊Bundles。

BundleConfig.RegisterBundles(BundleTable.Bundles);
4、說明

  只有在生產環境,修改js、css檔案才會產生版本雜湊值。

  在生產環境,使用bundles的方式會自動引用壓縮版min檔案。

5、說明

  偵錯模式

  生產環境

 

MVC-採用Bundles方式引入css和js檔案

相關文章

聯繫我們

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