標籤:
參考:http://www.cnblogs.com/madyina/p/3702314.html
http://blog.csdn.net/zhou44129879/article/details/16818987
功能描述:
1. @Styles.Render("~/content/css") ,@Style.Render("~/Content/css1","~/Content/css2"[,...]):
頁面載入指定CSS檔案
[email protected]("~/content/script") ,@Scripts.Render("~/xpath/script1","~/xpath/script2"[,...]):
頁面載入指定JS檔案
添加步驟:
1. APP_START下添加BundleConfig.cs,設定需綁定的檔案
using System.Web;using System.Web.Optimization;namespace XXX{ 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/jquery-min").Include( "~/Scripts/jquery-{version}.min.js")); bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap-{version}.min.js")); bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/style.css")); } }}
2.Global.asax配置內容
Application_start添加BundleConfig相關內容
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Http;using System.Web.Mvc;using System.Web.Routing;using System.Web.Optimization;namespaceXXX{ public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); BundleTable.EnableOptimizations = true;// 預設為true,生產環境下,會對css和js等檔案進行捆綁和壓縮,開發模式不執行該操作 BundleConfig.RegisterBundles(BundleTable.Bundles); RouteConfig.RegisterRoutes(RouteTable.Routes); } }}
3. 頁面新增內容
<head> <meta http-equiv="content-type" content="text/html;charset="utf=-8";width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> @Styles.Render("~/Cotent/css") @Styles.Render("~/bundles/modernizr") @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap")</head>
ASP.NET MVC4 @style.render