@Scripts.Render 中数组索引超出范围

13

对于每个控制器,需要有一个同名的文件夹(to-folder),并且对于每个动作都需要有一个脚本文件。

Folder structure

为每个文件创建一个bundle遵循以下模式: "~/Scripts/Controllers/{controller-name}/{filename-without-extension}"

bundles.IncludePerFile(new DirectoryInfo(server.MapPath("~/Scripts/Controllers")), "~/Scripts/Controllers/{0}/{1}", 
    (dir,file) => string.Format("~/Scripts/Controllers/{0}/{1}", dir, Path.GetFileNameWithoutExtension(file)), "*.js");

IncludePerFile是我创建的扩展方法,用于执行此任务。

然后应该存在一个包: ~/Scripts/Controllers/processos/pasta!
确认一下:

Bundle exist

到目前为止都是正确的!bundle 存在!

运行应用程序

当我运行应用程序时,会出现以下错误:

Bundle error

完整图像

错误且低效的修复方法:

如果我更改这个:

@Scripts.Render("~/Scripts/Controllers/processos/pasta")

到这个:

@Scripts.Render("~/Scripts/Controllers/processos/pasta.js")

没有产生错误。但是文件没有被压缩,因为实际上存在一个捆绑包。(我已经将应用程序设置为发布模式并发布了!)

完整错误信息

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

你找到答案了吗? - Sanj
我遇到了这个错误。恶意捆绑包含标准的MVC5 Bootstrap js资源。 - nVentimiglia
1
答案非常简单 :) 您需要更新您的软件包(Microsoft Web Optimization和WebGrease)http://fsou1.com/styles-render-index-was-outside-the-bounds-of-the-array/ - Maxim Zhukov
6个回答

33

5
我会核实您的所有元素是否都已经完成。
IncludePerFile(new DirectoryInfo(server.MapPath("~/Scripts/Controllers")), "~/Scripts/Controllers/{0}/{1}", 
(dir,file) => string.Format("~/Scripts/Controllers/{0}/{1}", dir, Path.GetFileNameWithoutExtension(file)), "*.js") 

他们在创建的bundle中放入了你认为他们放入的东西。

当错误类型的文件被包含在bundle中时,我在运行时遇到了这个错误。即:

@Styles.Render("~/bundles/myStyleBundle");

实际上包含一个JavaScript文件。

5

我曾经遇到相同的问题,尝试了不同的检查方法后发现我的css文件中有一个选择器造成了问题:

.glyphicons-icon _:-o-prefocus, .glyphicons-icon { background-image: url(../images/glyphicons.png); }

看起来是Microsoft.Ajax.Utilities.CssParser与_: css选择器存在问题。我删除了这一行代码,现在它可以正常工作了。


1
非常感谢,我正在使用从他们的网站下载的自定义Bootstrap,并且它有这个选择器。看起来很奇怪一个CSS选择器会导致黄屏死亡,但我想这是CSS解析引起的。 - Underscore

1
我刚遇到了类似的问题。我使用VS 2013和MVC 5,并更新到Bootstrap v3.0.1以使用Bootswatch主题。我使用最新的Bootstrap下载更新了所有内容,网站似乎工作正常。然后,我从Bootswatch网站获取了Slate的CSS,并在新样式表中使用它,在StyleBundle中进行了更改并构建解决方案。当我运行它时,出现“索引超出数组界限”的错误。切换回bootstrap.css,它就可以正常工作。
然后我使用NuGet Package Manager更新了所有软件包...我刚安装了VS 2013并没有更新所有内容。重新构建解决方案,一切都很好。所以我会支持更新您的软件包答案。

1

您可能需要更新一些NuGet库。尝试更新WebGrease。


0

我正在使用

@Scripts.Render()

对我来说,这是一个JS文件中的反引号问题。

示例1

    let something = `some text
bla bla
`;

上述代码抛出了这个异常。
 Index and length must refer to a location within the string.

样本2
let something = `some text
bla bla`;

现在它运行良好,所以不要在新行的开头保留闭合反引号...
要在您的js文件中找到这些情况,请按照以下步骤进行操作:
1- 按下ctrl+shift+f
2- 搜索`\r\n``
3- 启用正则表达式 4- 仅在js文件中搜索

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接