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

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - 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