Is there a way to update asp.net mvc bundle contents dynamically at run-time?

a 夏天 提交于 2019-12-05 03:55:15

It's bit late, but I'm just sharing my experience on my own questions here.

As discussed in the comments of the question, bundles are defined as part of a cs file (generally BundleConfig.cs inside App_Start). So, the bundles are defined at compile time, and at application start they will get added to collection and become usable.

Now, the interesting bit. At run-time, the optimization framework looks into the included files and creates a hash of the contents, and appends that as a version query-string to the bundle request. So, when the bundle is called the generated uri is like the below one.

http://example.com/Bundles/MyBundledScripts?v=ILpm9GTTPShzteCf85dcR4x0msPpku-QRNlggE42QN81

This version number v=... is completely dynamic. If any file content within the bundle is changed, this version will be regenerated, and will remain same otherwise.

Now to answer the questions,

[1] This is done automatically by the framework, no need to do anything extra for this. Every time a file content is changed, new version number will be generated and the clients will get the updated scripts.

[2] Not possible. If files included in a bundle are changed, is has to be recompiled.

[3] Yes, it can be used. The custom version number can be added as below.

@Scripts.Render("~/Bundles/MyBundledScripts?v=" + ConfigurationManager.AppSettings["ScriptVersion"])

But Caution! This will remove the automatic versioning based on file contents.

And, additionally, if there are multiple versions of the same file available and we always want to include the latest version available, that can be achieved easily by including a {version} wildcard in bundle configuration like below.

bundles.Add(new ScriptBundle("~/Bundles/MyBundledScripts")
            .Include(
                "~/Scripts/Vendor/someScript-{version}.js"
            ));

So, if there are 2 scripts in the /Scripts/Vendor folder someScript-2.3.js someScript-3.4.js

Then the file someScript-3.4.js (higher version) will get included automatically. And when a new file someScript-4.0.js is added to the folder, that will be served to clients without any need for recompile/restart.

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