【问题标题】:Running an ASP.NET MVC 4 app in release mode does not bundle and minifiy the js files在发布模式下运行 ASP.NET MVC 4 应用程序不会捆绑和缩小 js 文件
【发布时间】:2013-02-15 19:31:02
【问题描述】:

当我在 release 模式下运行我的 ASP.NET MVC 4 应用程序时,捆绑包仍会输出未缩小且单独的 js 文件,而不是将其捆绑并缩小为更少的捆绑 JavaScript 文件。

有什么想法吗?

仅供参考,发布配置:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

【问题讨论】:

标签: javascript asp.net-mvc asp.net-mvc-4 bundling-and-minification


【解决方案1】:

感谢 aleyush 的评论,Web.release.config 仅在发布应用程序时使用,而不是在本地运行时使用,我能够通过在 BundleConfig.cs 文件中添加以下行来修复它:

#if !DEBUG
BundleTable.EnableOptimizations = true;
#endif

由于 Debug 模式会定义 DEBUG 常量,而在 Release 模式下它没有被定义,这一行只会在 Release 模式下执行。 (可以通过设置断点来测试)

【讨论】:

    【解决方案2】:
    1. 如果在 Web.config 文件中将 debug 设置为 true,则不会捆绑或缩小任何内容,以便您可以轻松调试输出。

    2. 如果您想覆盖它,只需将以下代码行添加到您的 BundleConfig 文件中:

      BundleTable.EnableOptimizations = true;

    【讨论】:

    【解决方案3】:

    这对我有用

    <system.web>
        <compilation debug="false" />
    </system.web>
    

    【讨论】:

    • VS运行时会提示是否要开启debug,选择no即可!
    【解决方案4】:

    我的包裹太大了。我不得不把它分解成更小的部分,它工作得很好。缩小后可能有一些变量发生冲突。

    将此行放在 bundleconfig 的末尾仅用于测试...

    BundleTable.EnableOptimizations = true;
    

    如果你打开压缩后的文件,你会看到类似这样的内容。

        /* Minification failed. Returning unminified contents.
        (5079,1-2): run-time warning JS1195: Expected expression: .
        (5080,18-19): run-time warning JS1004: Expected ';': :
        (5084,18-19): run-time warning JS1004: Expected ';': :
        (5091,18-19): run-time warning JS1004: Expected ';': :
        (5095,20-21): run-time warning JS1197: Too many errors. The file might not be a JavaScript file: ;
    .....
    

    分解您的捆绑包,您可以隔离问题。

    希望这可以帮助某人。

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2012-08-10
      • 2021-01-17
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多