1

I am trying to use a Query String on my .js files to ensure they are reloaded everytime I make a change. I attempted to put the Query String onto the files as part of a ScriptBundle. When I click on a menu item that has a function within a .js that should have been loaded via the Bundle, the function is not found.

public static void RegisterBundles([NotNull] BundleCollection bundles)
    {
        var JAVASCRIPT_CSS_VERSION = System.Web.Configuration.WebConfigurationManager.AppSettings["JAVASCRIPT_CSS_VERSION"];

        BundleTable.EnableOptimizations = true;

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                   "~/Scripts/jquery-2.2.4.*"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery.browser.js?v=" + JAVASCRIPT_CSS_VERSION,
                    "~/Scripts/jquery-ui-1.8.24.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.js?v=" + JAVASCRIPT_CSS_VERSION));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/CrossMedia").Include(
                     "~/Scripts/CMApp/CrossMediaAjax.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/CrossMedia.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/WebMobile.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/SocialMedia.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/ENewsletter.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/AppData.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/PrintSummary.js?v=" + JAVASCRIPT_CSS_VERSION,
                     "~/Scripts/CMApp/Approvals.js?v=" + JAVASCRIPT_CSS_VERSION));

        bundles.Add(new ScriptBundle("~/bundles/brandview").Include(
                "~/Scripts/Brandview/Brandview.js?v=" + JAVASCRIPT_CSS_VERSION,

                "~/Scripts/Brandview/SelectTemplate.js?v=" + JAVASCRIPT_CSS_VERSION,
                "~/Scripts/Brandview/DataEntry.js?v=" + JAVASCRIPT_CSS_VERSION,
                // "~/Scripts/Brandview/Preview.js",
                // "~/Scripts/Brandview/ChartCtrl.js",
                "~/Scripts/Brandview/Submit.js?v=" + JAVASCRIPT_CSS_VERSION
                ));}

I know I haven't done this correctly because it doesn't work. In other instances that I've gotten QueryString with version to work is when I just use it as below.

 <script src="@Url.Content("~/Scripts/ABC/gladiola.js?v=" + JAVASCRIPT_CSS_VERSION)" type="text/javascript"></script>

Any help would be appreciated.

Thanks Bob

Bob Nona
  • 175
  • 1
  • 3
  • 11

1 Answers1

2

I continued researching and found this article and manipulated the code to my needs and it solved my problem.

MVC Cache Busting with Query String

Here's a snippet of my code now

bundles.Add(new ScriptBundle("~/bundles/brandview").Include(
                "~/Scripts/Brandview/Brandview.js",

                "~/Scripts/Brandview/SelectTemplate.js",
                "~/Scripts/Brandview/DataEntry.js",
                // "~/Scripts/Brandview/Preview.js",
                // "~/Scripts/Brandview/ChartCtrl.js",
                "~/Scripts/Brandview/Submit.js").WithVersionNumber());
    }

    private static Bundle WithVersionNumber(this Bundle sb)
    {
        sb.Transforms.Add(new LastModifiedBundleTransform());
        return sb;
    }
    private class LastModifiedBundleTransform : IBundleTransform
    {
        public void Process(BundleContext context, BundleResponse response)
        {

            var JAVASCRIPT_CSS_VERSION = System.Web.Configuration.WebConfigurationManager.AppSettings["JAVASCRIPT_CSS_VERSION"];

            foreach (var file in response.Files)
            {
                file.IncludedVirtualPath = string.Concat(file.IncludedVirtualPath, "?v=", JAVASCRIPT_CSS_VERSION);
            }
        }
    }
Bob Nona
  • 175
  • 1
  • 3
  • 11