mvc中Scripts.Render、Styles.Render


一、配置BundleConfig.cs文件

1、首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的css文件

2、BundleConfig就是一個微軟新加的 一個打包的配置類

3、BundleConfig用來Add 各種Bundle

4、BundleConfig配置信息如圖:

 
 
C# 代碼    復制

public class BundleConfig {  public static void RegisterBundles(BundleCollection bundles) {  bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));  bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include("~/Scripts/jquery-ui-{version}.js"));   bundles.Add(new StyleBundle("~/Content1/css").Include("~/Content/site.css"));  bundles.Add(new StyleBundle("~/Content1/themes/base/css").Include(  "~/Content/themes/base/jquery.ui.core.css",  "~/Content/themes/base/jquery.ui.resizable.css",  "~/Content/themes/base/jquery.ui.selectable.css",  "~/Content/themes/base/jquery.ui.accordion.css",  "~/Content/themes/base/jquery.ui.autocomplete.css",  "~/Content/themes/base/jquery.ui.theme.css"));  }  } 


5、配置信息說明

(1)、上面的"~/Content1" 是虛擬路徑,可以隨便起名,用於標記打包哪個文件夾下面的.css 文件,后面的Include方法接受的是一個string[] 根據傳入的路徑去對css文件進行打包。

(2)、使用Bundle來引用css有個好處 就是可以把多個css文件在一起請求,瀏覽器只發一次請求 不過必須在Global.asax里面 加一段代碼 BundleTable.EnableOptimizations = true 來啟用優化。

(3)、在啟用優化后,當頁面下次再次發送請求的時候 BundleConfig里面沒有更改的話 瀏覽器會從緩存中去取


 

二、使用Scripts.Render、Styles.Render引用BundleConfig中的配置

 

1、在視圖文件中使用Scripts.Render()輸出腳本包,Styles.Render()輸出樣式包

2、Script文件引用:@Scripts.Render(virtualPath[,virtualPath1][,virtualPath2][,...])

3、CSS文件引用:  @Styles.Render(virtualPath[,virtualPath1][,virtualPath2][,...])

4、實例

 


<!DOCTYPE html> <html> <head>  <meta charset="utf-8" />  <meta name="viewport" content="width=device-width" />  <title>@ViewBag.Title</title>  @Styles.Render("~/Content1/css") </head> <body>  @RenderBody()   @Scripts.Render("~/bundles/jqueryui") </body> </html> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM