English 中文(简体)
Index was outside the bounds of the array in @Scripts.Render
原标题:

For each controller have a folder (with the same name controler), and for each action a script file.

Folder structure

For each file is creating a bundle following the pattern: "~/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 is an extension method I created to perform this task

Then one bundle for: ~/Scripts/Controllers/processos/pasta should exist!
And to confirm this:

Bundle exist

So far so correct! The bundle exists!

Running app

When I run the application, the following error occurs:

Bundle error

Full image

Wrongly and inefficient to repair the error:

If I change this:

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

to this:

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

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

Full error

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:ClientscreditoimobiliariobbsistemasrcCreditoImobiliarioBBCreditoImobiliarioBB.WebViewsprocessosdisplay.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
问题回答

I ve had the same error in this question: StyleBundle Index was outside the bounds of the array

And answer is pretty simple: you have to update your Microsoft Web Optimization & WebGrease packages (at this time 1.1.2 & 1.6.0 versions)

I would verify that all the elements of your

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

are putting in the created bundles exactly what you think they re putting in.

I got this error at run time when the wrong type of file is included within a bundle. i.e.

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

actually contains a JavaScript file.

I had the same problem and when I tried different checking I found out that there is a selector in my css file like this that cause the problem:

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

it seems that Microsoft.Ajax.Utilities.CssParser has a problem with _: css selector. I removed the line and it s working.

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.

You may need to update some of your nuget libraries. Try updating WebGrease

I m using

@Scripts.Render()

For me it was an issue with the backtik in JS file.

Sample 1

    let something = `some text
bla bla
`;

Above code thrown this exception

 Index and length must refer to a location within the string.

Sample 2

let something = `some text
bla bla`;

Now it works well, so do not keep the closing backtik at the beginning new line ...

So to find this cases in your js files do the following:

1- Press ctrl+shift+f

2- search for ` ``

3- enable regex 4- search in js files only





相关问题
Index was outside the bounds of the array in @Scripts.Render

For each controller have a folder (with the same name controler), and for each action a script file. For each file is creating a bundle following the pattern: "~/Scripts/Controllers/{controller-name}/...

Persist querystring across requests in MVC 3

I have a page containing a list of clients with an ActionLink that allows the user to display (or hide) "inactive" clients. The showInactive=True is attached to the url as a querystring to the ...

How do I create a MVC Razor template for DisplayFor()

I have a couple of properties in my view model that are display-only but I need to retrieve their values using jQuery to perform a calculation on the page. The standard Html.DisplayFor() method just ...

Windows Workflow Foundation 4 and ASP.NET MVC

We are evaluating Windows Workflow Foundation 4 to use in MVC 3 based Web Applications. We would like to create flexible order workflows for different projects. Does anybody know good information ...

Asp.net MVC Authentication how does the Authentication work

May be my question is crazy. 1) ASP.net MVC is stateless, so there is no session involved in here. How does the authentication module work and do you have any articles which you can point me to ...

ASP.NET MVC Razor Concatenation

I m trying the render an HTML list that looks like the following, using the Razor view engine: <ul> <li id="item_1">Item 1</li> <li id="item_2">Item 2</li> </ul&...

Razor actionlink autogenerating ?length=7 in URL?

I have the link below on a razor page: @Html.ActionLink("Create New Profile", "Create", "Profile", new { @class="toplink" }) It appears in thes source of view page as shown below: <a href="/...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

热门标签