简体   繁体   English

捆绑和缩小-在调试模式下不引用脚本

[英]Bundling and Minification - Scripts are not referenced in debug mode

I am using bundling and minification feature of the web optimisation framework in a .NET MVC 5 application. 我在.NET MVC 5应用程序中使用Web优化框架的捆绑和缩小功能。

BundleConfig.cs: 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"));

     }

In the _Layout.cshtml file I am referencing the bundles like this: _Layout.cshtml文件中,我引用了这样的捆绑软件:

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

This works when in release mode, ie debug = false , without any exceptions in both server & client side. 这在释放模式下有效,即debug = false ,服务器端和客户端均无任何异常。

However, when I set the debug to true, none of the scripts are referenced in the page except for the front.js . 但是,当我将debug设置为true时,除front.js之外,页面中均未引用任何脚本。

I tried adding this to the Application_Start() event but I still experience the same issue: 我尝试将其添加到Application_Start()事件中,但仍然遇到相同的问题:

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

Here is the Web.config file: 这是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>

You need to change your script references from: 您需要从以下位置更改脚本引用:

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

to: 至:

"~/Scripts/something.js",

When debug is false the bundler will automatically look for the minified files (using the convention something.min.js ). 如果debugfalse则捆绑程序将自动查找缩小的文件(使用约定something.min.js )。 Otherwise, it will use the files listed. 否则,它将使用列出的文件。

Always point the bundler to non-minified files. 始终将捆绑程序指向未缩小的文件。 The minifier can take care of these, but it can't undo the minification if you point it to the minified copies. 缩小器可以解决这些问题,但是如果将缩小器指向缩小的副本,则无法撤消缩小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM