我想将我的JS脚本包含在捆绑包中,并同时版本,以避免在脚本被更改时避免浏览器缓存。
I want to include my JS scripts in a Bundle AND at the same time version them to avoid browser caching if those scripts have been changed.
使用 ASP.NET MVC 5 是否有 native 方式来实现这一目标?
Is there a "native" way with ASP.NET MVC 5 to accomplish this?
我找到了一个名为盒式磁带的库,它可以完成此操作(自动对JS进行版本控制一件好事),但我想简化一些事情(甚至),而无需太多更改我的脚本配置...
I've found a library called Cassette that does exactly this (automatically versioning the JS, which is a good thing) but I wanted something (even) simpler, without changing a lot my script configuration...
以下操作无效:
var version = ConfigurationManager.AppSettings["cache"].ToString(); bundles.Add(new ScriptBundle("~/bundles/scripts").Include( "~/Scripts/framework.js" + version, "~/Scripts/main.js"));结果是浏览器未下载 framework.js文件(可能不是
The result is that the "framework.js" file is not downloaded by the browser (probably is not found).
推荐答案MVC将自动为发布版本添加版本参数(即,如果web.config中的debug = false) );请参阅此处的捆绑缓存: www.asp/ mvc / overview / performance / bundle-and-minification
MVC will automatically add a version parameter for release builds (i.e. if debug="false" in web.config); see "Bundle Caching" here: www.asp/mvc/overview/performance/bundling-and-minification
您可以在调试版本中明确启用此功能(在同一篇文章中介绍),但是您的脚本会被缩小为同一动作的一部分;没有单独的方法。
You can explicitly enable this in debug builds (described in the same article), but your scripts will be minified as part of the same action; there's no separate method to do so.
更多推荐
如何在捆绑软件中对javascript文件进行版本控制?
发布评论