13

对于每个控制器都有一个文件夹(与控制器同名),并为每个动作一个脚本文件。

文件夹结构

对于每个文件,都按照以下模式创建一个包:“~/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应该存在!
并确认这一点:

捆绑存在

到目前为止都正确!捆绑包存在!

运行应用

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

捆绑错误

全图

错误且低效地修复错误:

如果我改变这个:

@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
4

6 回答 6

33

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

答案很简单:你必须更新你的Microsoft Web Optimization&WebGrease包(此时是 1.1.2 和 1.6.0 版本)

于 2014-02-17T20:46:21.583 回答
5

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

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

似乎 Microsoft.Ajax.Utilities.CssParser 的_: css 选择器有问题。我删除了这条线,它正在工作。

于 2014-11-02T04:24:07.420 回答
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") 

将创建的捆绑包放入您认为它们放入的包中。

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

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

实际上包含一个 JavaScript 文件。

于 2013-09-20T15:37:48.837 回答
1

I just ran into a similar problem. I am using VS 2013 and MVC 5 and was updating to Bootstrap v3.0.1 in order to use a theme from Bootswatch. I updated everything using the latest Bootstrap download and the site seemed to work fine. I then grabbed the CSS for Slate from the Bootswatch site and used it in the new stylesheet, changed it in the StyleBundle and built solution. When I ran it, I got the "Index was outside the bounds of the array" error. Switch back to bootstrap.css and it worked fine.

I then used NuGet Package Manager to update all my packages... I had just installed VS 2013 and not yet updated everything. Rebuilt the solution and wallah, it works great. So I would up vote the updating your packages answser.

于 2014-11-06T21:56:27.717 回答
1

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

于 2014-11-25T18:19:54.557 回答
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`;

现在它运行良好,所以不要在新行的开头保留结束的 backtik ...

于 2021-12-05T23:21:26.923 回答