How to version javascript files in a Bundle?

旧巷老猫 提交于 2019-12-22 10:14:10

问题


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.

Is there a "native" way with ASP.NET MVC 5 to accomplish this?

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...

The following does not work:

 var version = ConfigurationManager.AppSettings["cache"].ToString();
 bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
     "~/Scripts/framework.js" + version,
     "~/Scripts/main.js"));

The result is that the "framework.js" file is not downloaded by the browser (probably is not found).


回答1:


MVC will automatically add a version parameter for release builds (i.e. if debug="false" in web.config); see "Bundle Caching" here: http://www.asp.net/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.



来源:https://stackoverflow.com/questions/35901012/how-to-version-javascript-files-in-a-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!