【问题标题】:Can I use a QueryString version in a Script Bundle?我可以在脚本包中使用 QueryString 版本吗?
【发布时间】:2018-11-30 17:18:37
【问题描述】:

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

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>

任何帮助将不胜感激。

谢谢 鲍勃

【问题讨论】:

    标签: javascript c# css caching


    【解决方案1】:

    我继续研究并找到了这篇文章,并根据我的需要操作了代码,它解决了我的问题。

    MVC Cache Busting with Query String

    这是我现在的代码的 sn-p

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

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 2014-05-13
      • 1970-01-01
      • 2019-05-26
      • 2016-05-31
      • 2021-04-17
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多