title: Index was outside the bounds of the array in @Scripts.Render
body: <p>For each controller have a folder (with the same name controler), and for each action a script file.</p>
  
  <p><img src="https://i.stack.imgur.com/zh3GL.jpg" alt="Folder structure"></p>
  
  <p>For each file is creating a bundle following the pattern: <em>"~/Scripts/Controllers/{controller-name}/{filename-without-extension}"</em></p>
  
  <pre><code>bundles.IncludePerFile(new DirectoryInfo(server.MapPath("~/Scripts/Controllers")), "~/Scripts/Controllers/{0}/{1}", 
      (dir,file) =&gt; string.Format("~/Scripts/Controllers/{0}/{1}", dir, Path.GetFileNameWithoutExtension(file)), "*.js");
  </code></pre>
  
  <p><em><code>IncludePerFile</code> is an extension method I created to perform this task</em></p>
  
  <p>Then one bundle for: <code>~/Scripts/Controllers/processos/pasta</code> should exist!<br>
  <strong>And to confirm this:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/3Qvd9.jpg" alt="Bundle exist"></p>
  
  <p><strong>So far so correct! The bundle exists!</strong></p>
  
  <h2>Running app</h2>
  
  <p>When I run the application, the following error occurs:</p>
  
  <p><img src="https://i.stack.imgur.com/LEbJg.jpg" alt="Bundle error"></p>
  
  <blockquote>
    <p><a href="https://i.stack.imgur.com/LEbJg.jpg" rel="noreferrer">Full image</a></p>
  </blockquote>
  
  <h3>Wrongly and inefficient to repair the error:</h3>
  
  <p>If I change this:</p>
  
  <pre><code>@Scripts.Render("~/Scripts/Controllers/processos/pasta")
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>@Scripts.Render("~/Scripts/Controllers/processos/pasta.js")
  </code></pre>
  
  <p>No error is generated. But the file is not minified since there is effectively a bundle. (I have already put in release mode and published application!)</p>
  
  <h2>Full error</h2>
  
  <pre><code>Index was outside the bounds of the array.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.
  
  Source Error: 
  
  
  Line 3:  @section js {
  Line 4:     @*@Scripts.Render("~/Scripts/Controllers/processos/Pasta.js", "~/Scripts/Controllers/processos/display.js")*@
  Line 5:     @Scripts.Render("~/Scripts/Controllers/processos/pasta")
  Line 6:     @Scripts.Render("~/Scripts/Controllers/processos/display.js")
  Line 7:  
  
  Source File: w:\Clients\creditoimobiliariobb\sistema\src\CreditoImobiliarioBB\CreditoImobiliarioBB.Web\Views\processos\display.cshtml    Line: 5 
  
  Stack Trace: 
  
  
  [IndexOutOfRangeException: Index was outside the bounds of the array.]
     System.String.get_Chars(Int32 index) +0
     Microsoft.Ajax.Utilities.CssParser.Append(Object obj, TokenType tokenType) +402
     Microsoft.Ajax.Utilities.CssParser.AppendCurrent() +74
     Microsoft.Ajax.Utilities.CssParser.SkipToClose() +744
     Microsoft.Ajax.Utilities.CssParser.SkipToEndOfStatement() +232
     Microsoft.Ajax.Utilities.CssParser.ParseRule() +574
     Microsoft.Ajax.Utilities.CssParser.ParseStylesheet() +1235
     Microsoft.Ajax.Utilities.CssParser.Parse(String source) +897
     Microsoft.Ajax.Utilities.Minifier.MinifyStyleSheet(String source, CssSettings settings) +419
     System.Web.Optimization.CssMinify.Process(BundleContext context, BundleResponse response) +302
     System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context, String bundleContent, IEnumerable`1 bundleFiles) +207
     System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +355
     System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +104
     System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +254
     System.Web.Optimization.AssetManager.EliminateDuplicatesAndResolveUrls(IEnumerable`1 refs) +435
     System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +1029
     System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +75
     System.Web.Optimization.Scripts.RenderFormat(String tagFormat, String[] paths) +292
     System.Web.Optimization.Scripts.Render(String[] paths) +51
  </code></pre>