ASP.NET MVC4 @style.render

参考: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文件

2.@Scripts.Render("~/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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。