【问题标题】:Bundling and Minification - Scripts are not referenced in debug mode捆绑和缩小 - 在调试模式下不引用脚本
【发布时间】:2017-03-04 05:33:28
【问题描述】:

我在 .NET MVC 5 应用程序中使用 Web 优化框架的捆绑和缩小功能。

BundleConfig.cs:

    public static void RegisterBundles(BundleCollection bundles)
    {

        bundles.Add(new ScriptBundle("~/bundles/home").Include(
                  "~/Scripts/jquery-1.11.0.min.js",
                  "~/Scripts/bootstrap.min.js",
                  "~/Scripts/owl.carousel.min.js",
                  "~/Scripts/wow.min.js",
                  "~/Scripts/front.js"));

     }

在_Layout.cshtml 文件中,我像这样引用捆绑包:

    @Scripts.Render("~/bundles/home")

这在发布模式下有效,即debug = false,在服务器端和客户端都没有任何例外。

但是,当我将调试设置为 true 时,除了 front.js 之外,页面中没有引用任何脚本。

我尝试将此添加到 Application_Start() 事件中,但我仍然遇到同样的问题:

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

这是Web.config 文件:

<configuration> 
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="false" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="Cache1Week" duration="604800" varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
  <system.net>
    <mailSettings>
      <smtp from="local@mac.com">
        <network host="localhost" port="25"/>
      </smtp>
    </mailSettings>
  </system.net>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

</configuration>

【问题讨论】:

  • 在 RegisterBundles 中包含 if 逻辑而不是 Application_Start()
  • 你的意思是放到BundleConfig里面吗?如果是这样,那并没有什么不同。

标签: c# .net asp.net-mvc bundling-and-minification web-optimization


【解决方案1】:

您需要从以下位置更改脚本引用:

"~/Scripts/something.min.js",

到:

"~/Scripts/something.js",

当debug 是false 时,捆绑器将自动查找缩小的文件(使用约定something.min.js)。否则,它将使用列出的文件。

始终将捆绑程序指向非缩小文件。缩小器可以处理这些,但如果你将它指向缩小的副本,它就无法撤消缩小。

【讨论】:

  • 谢谢你!我相信约定只看文件名而不看内容,因为我刚刚从文件中删除了min 部分并且它正在工作。
  • 没错。该约定仅适用于文件名以及它是否已包含.min.js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 2012-09-19
  • 2012-08-10
  • 2012-11-30
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多