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

问题描述 投票:0回答:6

每个控制器都有一个文件夹(具有相同名称的控制器),每个操作都有一个脚本文件。

Folder structure

对于每个文件,都按照以下模式创建一个包:“~/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 error

Full image

修复错误错误且效率低下:

如果我改变这个:

@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
asp.net-mvc-4 asp.net-optimization
6个回答
33
投票

我在这个问题中遇到了同样的错误:StyleBundle Index was Outside of the array

答案非常简单:您必须更新您的

Microsoft Web Optimization
WebGrease
软件包(此时为 1.1.2 和 1.6.0 版本)


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

将创建的捆绑包放入您所认为的内容中。

当捆绑包中包含错误类型的文件时,我在运行时收到此错误。即

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

实际上包含一个 JavaScript 文件。


5
投票

我遇到了同样的问题,当我尝试不同的检查时,我发现我的 css 文件中有一个像这样的选择器导致了问题:

.glyphicons-图标_:-o-prefocus, .glyphicons-图标 { 背景图像:url(../images/glyphicons.png); }

似乎 Microsoft.Ajax.Utilities.CssParser 的 _: css 选择器有问题。我删除了线并且它可以工作。


1
投票

我刚刚遇到了类似的问题。我正在使用 VS 2013 和 MVC 5,并更新到 Bootstrap v3.0.1,以便使用 Bootswatch 的主题。我使用最新的 Bootstrap 下载更新了所有内容,该网站似乎工作正常。然后,我从 Bootswatch 网站获取了 Slate 的 CSS,并在新的样式表中使用了它,在 StyleBundle 中更改了它并构建了解决方案。当我运行它时,出现“索引超出数组范围”错误。切换回 bootstrap.css,效果很好。

然后我使用 NuGet Package Manager 更新我的所有包...我刚刚安装了 VS 2013,尚未更新所有内容。重建解决方案和wallah,效果很好。所以我会投票更新你的软件包答案。


1
投票

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


0
投票

我正在使用

@Scripts.Render()

对我来说,这是 JS 文件中的 backtik 的问题。

样品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- 搜索 ` ``

3-启用正则表达式 4-仅在js文件中搜索

© www.soinside.com 2019 - 2024. All rights reserved.