c# - 有没有办法以编程方式导出/保存生成的 javascript 和 css 包?

标签 c# javascript css .net-4.5 bundling-and-minification

我正在使用 System.Web.Optimization BundleTable 进行 CSS 和 JavaScript 文件捆绑。我可以通过转到 Chrome 开发人员工具Sources 选项卡,然后单击我要保存的 bundle ,然后在代码上单击鼠标右键,然后另存为(下面的屏幕截图)

enter image description here

我想知道是否有一种方法可以以编程方式保存它们(javascript或.NET,我不介意以太),因为我需要通过将它们与旧版本进行比较来测试捆绑文件中的更改以查看他们是否改变了。最终产品很可能会进行 Selenium 测试。

最佳答案

我自己整理了一下。

    public static string GetBundleContents(string virtualPath)
    {
        OptimizationSettings config = new OptimizationSettings()
        {
            ApplicationPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath,
            BundleTable = BundleTable.Bundles
        };

        BundleResponse response = Optimizer.BuildBundle(virtualPath, config);
        return response.Content;
    }

    public static void WriteBundlesToDisk(string path)
    {
        foreach (var bundle in BundleTable.Bundles)
        {
            var bundleContents = BundleConfig.GetBundleContents(bundle.Path);
            File.WriteAllText(string.Format("{0}/{1}.{2}", path, bundle.Path.Split('/').Last(), BundleConfig.GetFileExtensionByBundleType(bundle)), bundleContents);
        }
    }

    public static string GetFileExtensionByBundleType(Bundle bundle) 
    {
        if (bundle is ScriptBundle)
            return "js";
        else if (bundle is StyleBundle)
            return "css";
        return "folderBundle";
    }

用法:

BundleConfig.WriteBundlesToDisk("c://bundles");

旁注:有一种名为 System.Web.Optimization.DynamicFolderBundle 的 bundle 类型,上面的解决方案无法正确处理它,它会将其另存为 .folderBundle文件类型。

关于c# - 有没有办法以编程方式导出/保存生成的 javascript 和 css 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389114/

相关文章:

c# - 如何从主机类型为 ("Moles"的测试中读取 UnitTest 项目的 App.Config)

c# - LambdaExpression 为覆盖的属性获取不正确的 DeclaringType

c# - 为什么 foreach 循环在 gridview 的最后一行失败?

javascript - 我应该如何使用 PHP Excel 将 MYSQL 数据导出到 Excel

javascript - 无法让 slider 使用 4 个元素?

html - HTML UL LI 元素(列表元素)有多贵?

c# - 从 MySql 数据创建刻度盘和图表

javascript - 克隆上一个 tr 行并将其附加到当前表

css - 复选标记 "✔"在 Microsoft Edge 中看起来不同

html - 如何将 <svg> 元素与文本对齐?