Directory does not exist. Parameter name: directoryVirtualPath

82,889

Solution 1

I had the same problem and found out that I had some bundles that pointed to non-exisiting files using {version} and * wildcards such as

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    "~/Scripts/jquery-{version}.js"));

I removed all of those and the error went away.

Solution 2

I had this same issue and it was not a code problem. I was using the publish option (not the FTP one) and Visual Studio was not uploading some of my scripts/css to the azure server because they were not "included in my project". So, locally it worked just fine, because files were there in my hard drive. What solved this issue in my case was "Project > Show all files..." and right click the ones that were not included, include them and publish again

Solution 3

Here's a quick class I wrote to make this easier.

using System.Web.Hosting;
using System.Web.Optimization;

// a more fault-tolerant bundle that doesn't blow up if the file isn't there
public class BundleRelaxed : Bundle
{
    public BundleRelaxed(string virtualPath)
        : base(virtualPath)
    {
    }

    public new BundleRelaxed IncludeDirectory(string directoryVirtualPath, string searchPattern, bool searchSubdirectories)
    {
        var truePath = HostingEnvironment.MapPath(directoryVirtualPath);
        if (truePath == null) return this;

        var dir = new System.IO.DirectoryInfo(truePath);
        if (!dir.Exists || dir.GetFiles(searchPattern).Length < 1) return this;

        base.IncludeDirectory(directoryVirtualPath, searchPattern);
        return this;
    }

    public new BundleRelaxed IncludeDirectory(string directoryVirtualPath, string searchPattern)
    {
        return IncludeDirectory(directoryVirtualPath, searchPattern, false);
    }
}

To use it, just replace ScriptBundle with BundleRelaxed in your code, as in:

        bundles.Add(new BundleRelaxed("~/bundles/admin")
            .IncludeDirectory("~/Content/Admin", "*.js")
            .IncludeDirectory("~/Content/Admin/controllers", "*.js")
            .IncludeDirectory("~/Content/Admin/directives", "*.js")
            .IncludeDirectory("~/Content/Admin/services", "*.js")
            );

Solution 4

I ran into the same issue today it is actually I found that some of files under ~/Scripts is not published. The issue is resolved after I published the missing files

Solution 5

I also got this error by having non-existant directories in my bundles.config file. Changing this:

<?xml version="1.0"?>
<bundleConfig ignoreIfDebug="true" ignoreIfLocal="true">
    <cssBundles>
        <add bundlePath="~/css/shared">
            <directories>
                <add directoryPath="~/content/" searchPattern="*.css"></add>
            </directories>
        </add>
    </cssBundles>
    <jsBundles>
        <add bundlePath="~/js/shared">
            <directories>
                <add directoryPath="~/scripts/" searchPattern="*.js"></add>
            </directories>
            <!--
            <files>
                <add filePath="~/scripts/jscript1.js"></add>
                <add filePath="~/scripts/jscript2.js"></add>
            </files>
            -->
        </add>
    </jsBundles>
</bundleConfig>

To this:

<?xml version="1.0"?>
<bundleConfig ignoreIfDebug="true" ignoreIfLocal="true">
    <cssBundles>
    </cssBundles>
    <jsBundles>
    </jsBundles>
</bundleConfig>

Solve the problem for me.

Share:
82,889
BjarkeCK
Author by

BjarkeCK

Updated on November 05, 2020

Comments

  • BjarkeCK
    BjarkeCK over 3 years

    i just published my project to my host on Arvixe and get this error (Works fine local):

    Server Error in '/' Application.
    
    Directory does not exist.
    Parameter name: directoryVirtualPath
    
    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.ArgumentException: Directory does not exist.
    Parameter name: directoryVirtualPath
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace: 
    
    
    [ArgumentException: Directory does not exist.
    Parameter name: directoryVirtualPath]
       System.Web.Optimization.Bundle.IncludeDirectory(String directoryVirtualPath, String searchPattern, Boolean searchSubdirectories) +357
       System.Web.Optimization.Bundle.Include(String[] virtualPaths) +287
       IconBench.BundleConfig.RegisterBundles(BundleCollection bundles) +75
       IconBench.MvcApplication.Application_Start() +128
    
    [HttpException (0x80004005): Directory does not exist.
    Parameter name: directoryVirtualPath]
       System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9160125
       System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +131
       System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194
       System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339
       System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253
    
    [HttpException (0x80004005): Directory does not exist.
    Parameter name: directoryVirtualPath]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9079228
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.237
    

    What does it mean ?

  • CrazyPyro
    CrazyPyro over 10 years
    Not sure how this is "amazingly obscure" or hard to find; the stacktrace points you straight to the BundleConfig.RegisterBundles call from Application_Start My +1 goes to @user2465004 's answer instead.
  • CrazyPyro
    CrazyPyro over 10 years
    +1 This is a much better answer than the accepted one, and should maybe be merged into it. Since the first logical thing to do in response to a "file/directory not found" is already to go check that it exists. But in this situation it's a bit sneakier, because you check and it does exist locally, just not on the server. For an even weirder situation, see my answer.
  • user1616625
    user1616625 about 10 years
    I received the same error because the /scripts/ folder referred to in my bundles did not exist on my server.
  • stimms
    stimms about 10 years
    I also had this issue. Deploying from my local box worked but from the build server it didn't. Turned out to be that the build server wasn't including the .js files generated by the TypeScript compiler in the package. Likely an older version of the TypeScript tools on the build server. As a quick fix I included the .js files in the project.
  • Filip
    Filip almost 10 years
    For me it was a problem with BitTorrent Sync used to deploy files. Some files were simply not deployed due to some glitch..
  • Sam
    Sam about 9 years
    I converted a asp.net mvc project to web api and really had no use of jquery, css files. Glad I found your post. Fixed it and everything is working fine.
  • RamblerToning
    RamblerToning about 9 years
    In addition to this, when publishing to Azure it doesn't seem to let you publish empty folders. I had a .IncludeDirectory("~/Scripts/Create/Controllers", "*.js") statement, and while the Controllers folder did indeed exist it didn't actually have anything in it yet and this caused the same error. I just put a blank text file in the folder, and then it worked.
  • JMK
    JMK almost 9 years
    This happened to me when I had empty directories included in my bundle config, which I planned to add files to in the future. Everything was fine locally because those directories existed, but when I pushed to Azure, they didn't get created,
  • SliverNinja - MSFT
    SliverNinja - MSFT about 8 years
    Great example - only gotcha here is that HostingEnvironment.MapPath does not take into consideration BundleTable.VirtualPathProvider extensions you may be using (may be non-default and not HostingEnvironment.VirtualPathProvider). For this case, you would want to convert the example above to use BundleTable.VirtualPathProvider.DirectoryExists and BundleTable.VirtualPathProvider.GetDirectory. File Pattern searches become a bit more problematic, but good place to start.
  • AceMark
    AceMark over 7 years
    the comment from @RamblerToning worked for me. it threw an error on empty directories.
  • Don Rolling
    Don Rolling over 7 years
    This fixed the issue for me. Still haven't figured out who the offending bundle is. Thank you for this powerful code sample, you saved me from further aggravation this afternoon.