我可以在脚本包中使用QueryString版本吗?

时间:2018-11-30 17:18:38

标签: javascript c# css caching

我正在尝试对.js文件使用查询字符串,以确保每次进行更改时都重新加载它们。我试图将查询字符串作为ScriptBundle的一部分放到文件上。当我单击菜单项中具有应通过捆绑包加载的.js中的功能时,找不到该功能。

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
                ));}

我知道我没有正确执行此操作,因为它不起作用。在其他情况下,当我按如下所述使用QueryString时,版本就可以使用。

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

任何帮助将不胜感激。

谢谢 鲍勃

1 个答案:

答案 0 :(得分:0)

我继续研究并找到了这篇文章,并根据自己的需要操纵了代码,从而解决了我的问题。

MVC Cache Busting with Query String

这是我的代码段

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);
            }
        }
    }