asp.netvisual-studiosingle-page-applicationasp.net-optimization

ScriptBundle IncludeDirectory Length Maximum For Filenames?


Is there a maximum filename length in ASP.NET's ScriptBundle .IncludeDirectory method when search sub directories is enabled?

When I include a file that has a long descriptive name and then launch the project my Visual Studio 2013 Professional starts to load everything, but once it starts to load the .js files VS just hangs without ever loading the website in the browser. However, when I trim the longer names of the .js files in the folders that I've used .IncludeDirectory it seems to work well. But I could be off. Has anyone else encountered this sort of behavior? How did you fix it?

Simplified Example

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/angularapp")
         .IncludeDirectory("~/AngularApp", "*.js", true));
}  

Folders

Obviously, my project contains a lot more files and a much deeper file structure than this. But I'm trying to transition to .IncludeDirectory from explicitly including each .js file in a very large .Include block.


Solution

  • I have a structure like this:

    enter image description here

    I tried to use this:

        bundles.Add(new ScriptBundle("~/bundles/myScripts").Include(
            "~/Scripts/ng-google-chart.js",
            "~/Scripts for myapp/my_scripts.js"
            )               
            // WARN: this one fail
            .IncludeDirectory("~/Scripts for myapp/Angular", "*.js", true)
        );
    

    It fails. If I use IncludeDirectory in a separate rows it fails on:

    @Scripts.Render("~/bundles/myscripts")
    

    The Angular directory contains only .js files and there are not empty directories neither strange characters in the names apart from the underscore.

    I solved using the .IncludeDirectory sintax without the searchSubdirectories parameter, a call for every subdirectories.

    ScriptBundle myBundle = bundles.GetBundleFor("~/bundles/myScripts") as ScriptBundle;
    myBundle.IncludeDirectory("~/Scripts for myapp/Angular/controllers", "*.js");
    myBundle.IncludeDirectory("~/Scripts for myapp/Angular/directives", "*.js");
    [...]
    

    It works, so the paths and files are correct. It is just the method that search in the subdirectories that fails.

    I assume you are calling

    BundleConfig.RegisterBundles(BundleTable.Bundles);
    

    in Application_Start of the Global.asax .

    To find what is the error try to attach the debugger and put a break point somewhere before that call. Probably you'll have a System.Web.HttpException (actually one for every subdirectory) that says:

    Invalid file name for file monitoring: '[path to your application]\mywebapp\Scripts for myapp\Angular\controllers'. Common reasons for failure include: - The filename is not a valid Win32 file name. - The filename is not an absolute path. - The filename contains wildcard characters. - The file specified is a directory. - Access denied.

    In my case none of these reasons seems valid, I can register the content of the subfolders using the method without the "subfolder search". (Tip. To make Application_Start fired again just add/remove a blank line in web.config.)

    Note that this exception does not block the ASP engine and in my case the page is rendered in the browser, you just doesn't see the error. I noticed it because in the Application_Start I do other operations and them are not executed.

    I don't know the real cause of the error, but this is how you can find it and the workaround that works for me.

    Tip. To exclude the Jasmine test files I use this (after the other commands): bundles.IgnoreList.Ignore("*_jasmine.js", OptimizationMode.Always);