惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
博客园_首页
月光博客
月光博客
N
News | PayPal Newsroom
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
G
Google Developers Blog
T
Troy Hunt's Blog
博客园 - Franky
腾讯CDC
S
Security Affairs
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
D
DataBreaches.Net
Recorded Future
Recorded Future
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
D
Docker
P
Proofpoint News Feed
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

博客园 - sinlight23

Web开发框架趋势 ASP.NET MVC - PageData的应用 ASP.NET拾遗 - Health Monitoring .NET Xml序列化时会忽略值为"默认值"的Property或Field ASP.NET MVC 实现模式 - ModelBuilder Enumerable.Range和自定义的IntRange/DateRange HOWTO: Web Deploy时服务器报登录失败的解决方法 HOWTO: 为GitHub for Windows指定代理服务器 脚印:关于错误编码的管理的一些思考 脚印:软件开发随想录 脚印:关于扩展方法的使用 脚印:一次重构讨论 脚印:记录一次重构,将规则生产和规则消费(执行委托)分离 HOWTO: IE8下处理iframe自适应高度 Microsoft ASP.NET 2.0 AJAX 相关信息备忘 MVC 模式在javascript中的应用 VS2010 "SQL Server 2005 Database Project" 使用笔记(二) VS2010 "SQL Server 2005 Database Project" 使用笔记 腳印: 初學者的心態
ASP.NET MVC - 在MVC 3项目中使用ASP.NET Bundling and Minification机制
sinlight23 · 2012-12-04 · via 博客园 - sinlight23

System.Web.Optimization是在ASP.NET 4.5中正式引入的。

1. 在MVC 3中如何使用Bundling and Minification机制

个人建议的方法,是在VS中建一个MVC 4的项目,看看Global.asax.cs里多了什么,看看App_Start目录下多了什么。

MvcApplication.Application_Start() in Global.ascx.cs

BundleTable.EnableOptimizations = `true`;  
`BundleConfig.RegisterBundles`(BundleTable.Bundles);

[内容已过期] http://joeyiodice.com/using-mvc4-minification-and-bundling-in-mvc3
http://forums.asp.net/t/1799992.aspx/1
在MVC 4中使用的官方教程

2. 重要特性总结(待补充)

  1. Bundle使用了OutputCache
  2. 对物理文件修改后,文件变化可以立刻输出。原理是使用了缓存依赖,源文件作为依赖项。
  3. 文件内容相同的请求,v参数的值不变,因此可能会使用上浏览器内的客户端缓存。
  4. Bundle不能定义对不同domain的文件的引用。能否定义同域的但路径为虚拟路径的文件的引用,未知。

3. 动态Bundle

BundleApplication_Start时被注册到BundleTable.Bundles中。这样做的好处是,(预定义的)Bundle可以被重用。

但如果仅有这一条途径注册Bundle,有时候会觉得未免太繁琐:

1)修改BundelConfig.cs

2)重新编译项目

3)忍受站点启动缓慢的过程。

假如你遇到的情形是,某个页面上需要一个偶尔使用的一个或多个js,你想利用**Bundling and Minification机制**对其打包或启用压缩,有没有简单点的办法呢?

看下别人的解决方案:

前面的问题很好的解决了。现在试着想一个新问题,这样做是否就无法满足**重用Bundle**的需求呢?如果你了解MVC 2,可以试着用MVC 2中引入的DisplayTemplates机制解决这个问题。

4. Sample

静态Bundle

// 注册
public static void RegisterBundles(BundleCollection bundles)
{
    var styleBundle = new StyleBundle("~/Content/css").Include(
        "~/css/reset.css",
        "~/css/common.css",
        "~/css/layout.css"
    );
    bundles.Add(styleBundle);
}

@*使用*@
@Styles.Render("~/Content/css")

动态Bundle

// 定义
public static IHtmlString DynamicScriptsBundle(this HtmlHelper htmlHelper, string nombre, params string[] urls)
{
    string path = string.Format("{0}", nombre);
    if (BundleTable.Bundles.GetBundleFor(path) == null)
        BundleTable.Bundles.Add(new ScriptBundle(path).Include(urls));
    return Scripts.Render(path);
}

asp-net-mvc-4-use-bundles-beneficts-for-url-content
runtime-bundling-and-minifying-in-mvc-4

@*使用*@
@Html.DynamicScriptsBundle("~/Scripts/Common", "~/js/common.js")

延伸阅读

a-localized-scriptbundle-solution