ASP.NET MVC4 @style.render

來源:互聯網
上載者:User

標籤:

參考: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

聯繫我們

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