mvc4.0 @Styles.Render
来源:互联网 发布:地铁线路运行 编程 编辑:程序博客网 时间:2024/05/29 11:39
最近公司的新项目用了MVC 4.0,接下来一步步把 工作中遇到的问题 总结起来..
1.@Styles.Render
在页面上可以用@Styles.Render("~/Content/css") 来加载css
首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的css文件
BundleConfig就是一个微软新加的 一个打包的配置类
用来Add 各种Bundle
bundles.Add(new StyleBundle("~/Content").Include("~/Content/common.css")); //这样是错误的 new StyleBundle("~/Content") 初始化的虚拟目录名称不能跟 真正的目录相同 也就是 后面的Include("~/Content/common.css")); 这里要把new StyleBundle("~/Content")的"~/Content" 改成别的名称。
bundles.Add(new StyleBundle("~/Content1").Include("~/Content/common.css", "~/Content/content.css"));bundles.Add(new StyleBundle("~/Content2").Include("~/Content/site.css" ));
这里的"~/Content1" 可以随便起名(但一定要按这个URL格式来) 用于标记打包哪个文件夹下面的.css 文件,后面的Include方法接受的是一个string[] 根据传入的路径去对css文件进行打包
然后前面页面用@Styles.Render("~/Content1”,"~/Content2”) 来调用显示。
bundles.Add(new StyleBundle("~/Content1").Include("~/Content/content.css")); bundles.Add(new StyleBundle("~/Content1").Include("~/Content/site.css")); bundles.Add(new StyleBundle("~/Content1").Include("~/Content/common.css"));
这种情况添加相同的Key时 程序会调用最后一次添加的~/Content/common.css。
当然也可以直接在页面上来加载比如:
(2).@Styles.Render("~/Content/site.css", "~/Content/common.css", "~/Content/content.css")
或者 第一种方式+第二种方式组合来加载 比如:
BundleConfig里面添加了一个
bundles.Add(new StyleBundle("~/Content1").Include("~/Content/common.css"));
组合调用 :@Styles.Render("~/Content1", "~/Content/site.css", "~/Content/content.css")
这时候页面上会加载3个css文件
但是 如果BundleConfig里面这样
bundles.Add(new StyleBundle("~/Content1").Include("~/Content/common.css","~/Content/content.css"));
页面上这样写:@Styles.Render("~/Content1", "~/Content/site.css", "~/Content/content.css")
看到没这个"~/Content/content.css"重复了, 2边都引用了.. 这时候 程序不会再执行页面上引用的css
注意:使用Bundle来引用css有个好处 就是可以把多个css文件在一起请求,浏览器只发一次请求 不过必须在Global.asax里面 加一段代码 BundleTable.EnableOptimizations = true;
来启用优化,看最终结果
而且 当页面下次再次发送请求的时候 BundleConfig里面没有更改的话 浏览器会从缓存中去取 ,这一点大大提高了性能 ...
304 Not Modified 表示数据无变化没更新 下面32.3kb来自缓存
- mvc4.0 @Styles.Render
- mvc4.0 @Styles.Render
- mvc4.0 @Styles.Render
- @Styles.Render
- Styles.Render(用法)
- mvc中Scripts.Render、Styles.Render
- Styles.Rende @Scripts.Render 错误
- Mvc4_@Styles.Render提高性能
- 由 Styles.Render 所引发 runAllManagedModules
- Styles.Render索引超出了数组界限-搞定
- styles
- styles
- Styles
- ASP.NET MVC4视图中出现 上下文中不存在 @Styles @Scripts
- vue2.0 Render函数 Render具体能做点什么 Render应用场景 Render在.vue无法使用
- Render;
- render
- MVC4.0 WEDAPI(1)
- 470例 Winform界面特效的源码
- Android Google Map学习网址
- python list内object排序
- 软件开发:速度 vs 质量
- 完成删除功能
- mvc4.0 @Styles.Render
- storm的日志问题
- PLSQL Developer 配置方法
- C#2010 在使用 System.Data.SQLite.dll 时出现异常
- 高效程序员的 7 个共同特征
- Android 定位系统(GPS)开发基础
- 判断用户名是否可用的向量版
- source insight中文乱码
- 包和访问权限(3)