启用 MVC 5.1 调试不会禁用捆绑和缩小



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

如果编译模式设置为debug="true",则应该禁用绑定和缩小,但没有。当我检查页面上的View源代码时,样式和脚本是绑定的
<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")

您可以看看这篇文章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(中定义脚本捆绑包时,您可以使用基本捆绑包类,如

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

我在发布版本中也看到了这一点。为了避免这种情况,我使用了条件标志来实现同样的效果。

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

最新更新