Darren Darren - 2 months ago 37
ASP.NET (C#) Question

ASP.Net MVC Script Bundle results in 404

I've searched SO - found many of the same question, though none of the answers helped.

I've built a bunch of sites and not ran into this issue before.

Essentially, my script bundle results in a 404 for each of the files in my javascript folder.

My structure (at the moment, i've changed it a bunch!) looks like this:

enter image description here

I do this so i can guarantee that ASP.Net doesn't change the order - i can ensure certain scripts are ahead of others. It's how i've always done it and it normally works well.

My bundle script - at the moment - is:

public static void RegisterBundles(BundleCollection bundles)
{
bundles.FileSetOrderList.Clear();
// stlyes
StyleBundle cssBundle = new StyleBundle("~/bundles/css");
cssBundle.IncludeDirectory("~/content/css", "*.css", true);
bundles.Add(cssBundle);


//scripts
ScriptBundle jsBundle = new ScriptBundle("~/bundles/jscript");
jsBundle.IncludeDirectory("~/content/javascript", "*.js", true);
bundles.Add(jsBundle);

}


I have tried a whole bunch of virtual paths.

My CSS loads perfect. My Js - i get a list of 404's; one for each of the */js files.

Any ideas?

My console looks like this - which also shows me that
bundles.FileSetOrderList.Clear();
isn't actually clearing its list else i would have jquery before angular (as is my intent)

enter image description here

UPDATE

If i
BundleTable.EnableOptimizations = true;
in my bundles then it's all bundled, minified and works - though this sucks for development debugging - what on earth is preventing it working in debug mode?!

Answer

This post seems to describe the same problem ASP.Net MVC 5 sub-directory bundling issues and is a known issue with version 1.1.1 of the Bundling framework.

If you don't want to have to downgrade or upgrade to a version where this is working, you always have the option of explicitly adding files to the bundle that you want to come first. Let's say you have your files in the same folder.

/javascript/lib/ascript.js
/javascript/lib/ascript2.js
/javascript/lib/jquery.js
/javascript/lib/yscript.js

You can be explicit about the files you want first via Include(), and then still lump the rest together via IncludeDirectory().

bundles.Add(new ScriptBundle("~/bundles/jscript").Include(
    "~/javascript/lib/jquery.js",
    .IncludeDirectory("~/javascript/lib", "*.js")

The bundling is smart enough to not double include jQuery.js if it has been explicitly added first. Similarly, you can have multiple .IncludeDirectory calls on your various subdirectories if you want to still keep them sub-foldered.