码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC4 @style.render

时间:2015-02-10 00:28:17      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

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

 

ASP.NET MVC4 @style.render

标签:

原文地址:http://www.cnblogs.com/iszer/p/4282743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!