Is there a way to update the contents of an asp.net mvc package dynamically at runtime?

I am ASP.NET MVC v4 for my application and I am using the web optimization features (combining and minimizing scripts and styles).

Now, I understand (please correct me, if not), the optimization structure will consider the included files at compile time and configure them. It will create a version number (v = something) based on the content. Each time the content changes, it recreates the hash version and the client receives updated files.

Now, is there a way to do the following:

[1] Update something inside the js file on my server and do the update for clients without re-creating and restarting the application (I don’t change the package configuration here, just updating the contents of the file inside the script)?

[2] Update the script configuration (for example, adding a new script set to the kit)) and get access to these clients without having to recompile and re-view the application? Or at least without recompiling? (I know, as a rule, we define packages inside cs files, but we wonder if there is a way out!)

[3] Is there a way to use my own version number (say, from the configuration file, v = myCustomScriptVersion), and not for a hash with an automatically generated version?

+7
asp.net-mvc asp.net-mvc-4 bundling-and-minification web-optimization
source share
1 answer

It's a little late, but I'm just sharing my experience on my issues here.

As discussed in the comments on the question, bundles are defined as part of the cs file (usually BundleConfig.cs inside App_Start). Thus, packages are defined at compile time, and when the application starts, they will be added to the collection and become useful.

Now, an interesting bit. At run time, the optimization infrastructure looks at the included files and creates a hash of the content and adds this as a version request string to the package request. So, when a bundle is called a generated uri, it is similar to the one below.

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

This version number v = ... is completely dynamic. If any contents of the file in the package are changed, this version will be restored and will remain the same.

Now, to answer the questions,

[1] This is done automatically using the framework, you do not need to do anything for this. Each time you change the contents of the file, a new version number is created and clients receive updated scripts.

[2] Impossible. If the files included in the kit are changed, you need to recompile.

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

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

But watch out! This will remove automatic version control based on the contents of the file.

In addition, if several versions of the same file are available, and we always want to include the latest available version, which can be achieved easily by including the {version} wildcard in the bundle configuration, as shown below.

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

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

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

+7
source share

All Articles