Scripts.Render using outdated javascript file

2020-02-06 09:58发布

问题:

My MVC4 application is using Scripts.Render to load a bundle that is loading a file called "functions.js."

When I debug this application in the browser, the script loads, but the version is outdated. When I view the resource directly, but append ?v=anytext the script looks correct, but without that appended, the script shows the old code. Is there a way to force the bundling to output the correct file instead of a stale one?

回答1:

This is likely a caching issue. When using Bundling and Minification in debug mode (when <compilation debug="true" /> is set in your web.config), bundling/minification is disabled.

You can override this and force bundling and minification by adding BundleTable.EnableOptimizations = true;. This will make it act like it will in your production environment, where everything is bundled and minified, and the script reference includes that versioning parameter (like you gave) that will force the browser to reload your scripts when anything changes.

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        //all your bundle code
        BundleTable.EnableOptimizations = true;
    }
}