【问题标题】:MVC 5.1 debug enabled doesn't disable Bundling and minification启用 MVC 5.1 调试不会禁用捆绑和缩小
【发布时间】:2014-07-05 14:38:48
【问题描述】:

在 VS 2013.2RTM Pro、MVC 5.1 应用程序的调试中运行。

如果编译模式设置为 debug="true" 它应该禁用捆绑和缩小但它没有。当我检查页面上的查看源代码时,样式和脚本是捆绑在一起的。
<script src="/bundles/modernizr?v=K-FFpFNtIXjnmlQamnX3qHX_A5r984M2xbAgcuEm38iv41"></script>

如果我在 BundleConfig.cs 中设置 BundleTable.EnableOptimizations = false;,它会禁用捆绑和缩小,但这不是它应该如何工作的。我不应该记得切换EnableOptimizations 设置!

在 VS 2012 MVC 4 应用程序中一切正常。

这是 MVC 5.1 的错误吗?有没有其他人有这个问题?有没有办法让调试禁用捆绑和缩小?

web.config:

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" useFullyQualifiedRedirectUrl="true" maxRequestLength="100000" enableVersionHeader="false" />
    <sessionState cookieName="My_SessionId" />
  <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
  </system.web>

_Layout.cshtml:

在标题中

@Styles.Render("~/Content/css") @Styles.Render("~/Content/themes/base/css") @Scripts.Render("~/bundles/modernizr")

在正文的末尾

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") @Scripts.Render("~/bundles/jqueryval")

【问题讨论】:

  • 也有这个问题
  • @Jason 在下面查看我的答案

标签: visual-studio-2013 bundling-and-minification asp.net-mvc-5.1


【解决方案1】:

你可以看看这篇文章 http://codemares.blogspot.com.eg/2012/03/disable-minification-with-mvc-4-bundles.html

或者你可以使用这个简单的实现

public class NoMinifyTransform : JsMinify
{
    public override void Process(BundleContext context, BundleResponse response)
    {
        context.EnableOptimizations = false;
        var enableInstrumentation = context.EnableInstrumentation;
        context.EnableInstrumentation = true;
        base.Process(context, response);
        context.EnableInstrumentation = enableInstrumentation;
    }
}

然后在 (App_Start) 中定义脚本包时,您可以像这样使用基本 Bundle 类

            IBundleTransform jsTransformer;
#if DEBUG
            BundleTable.EnableOptimizations = false;
            jsTransformer = new NoMinifyTransform();
#else
            jstransformer = new JsMinify();
#endif
            bundles.Add(new Bundle("~/TestBundle/alljs", jsTransformer)
               .Include("~/Scripts/a.js")
                .Include("~/Scripts/b.js")
                .Include("~/Scripts/c.js"));

【讨论】:

    【解决方案2】:

    我在发布版本中也看到了这一点。为了解决这个问题,我使用条件标志来实现相同的效果。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-26
      • 2019-03-05
      • 2018-05-11
      • 1970-01-01
      • 2015-02-26
      • 2013-02-21
      • 2014-05-22
      • 2017-03-04
      相关资源
      最近更新 更多