目录不存在。参数名称:directoryVirtualPath。

123

我刚刚将我的项目发布到了Arvixe上的主机,但遇到了这个错误(在本地工作正常):

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
什么意思?

请检查您是否已将脚本文件夹上传到服务器。 - Ninja
21个回答

241

我曾经遇到同样的问题,并发现有些捆绑包使用了 {version} 和 * 通配符指向不存在的文件。

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

我移除了所有这些代码,错误消失了。


2
不确定这个问题为什么被认为是“惊人的模糊”或难以找到;堆栈跟踪直接指向了Application_Start中的BundleConfig.RegisterBundles调用。 我支持@user2465004的答案。 - CrazyPyro
3
我收到了相同的错误,因为我的捆绑包中引用的/scripts/文件夹在我的服务器上不存在。 - user1616625
我将一个asp.net mvc项目转换为web api,实际上没有使用jquery和css文件。很高兴找到了你的帖子。修复后一切都正常工作。 - Sam
3
另外,在发布到Azure时,似乎不允许发布空文件夹。我使用了".IncludeDirectory(〜/Scripts/Create/Controllers","*.js")语句,虽然Controllers文件夹确实存在,但它实际上还没有任何内容,这导致了同样的错误。我只需在文件夹中放置一个空白文本文件,然后它就可以正常工作了。 - RamblerToning
当我在我的捆绑配置中包含空目录并计划将来添加文件时,这种情况发生在我身上。由于这些目录存在,本地一切正常,但是当我推送到Azure时,它们没有被创建。 - JMK
@RamblerToning的评论对我有用。在空目录上会抛出错误。 - AceMark

15

我曾遇到同样的问题,但它并不是代码问题。 我使用的是发布选项(而不是FTP),Visual Studio没有将我的某些脚本/ CSS上传到Azure服务器,因为它们没有“包含在我的项目中”。 因此,在本地工作正常,因为文件已经存在于硬盘中。 在我的情况下解决这个问题的方法是点击“项目 > 显示所有文件”,然后右键单击未包括的文件,将它们包含并重新发布。


+1 这是比被采纳的答案更好的答案,或许应该将其合并到采纳的答案中。因为对于“文件/目录未找到”的响应,第一件逻辑上要做的事情已经是去检查它是否存在了。但在这种情况下,它有点难以捉摸,因为你检查它,并且它确实存在于本地,只是服务器上没有。如果想看到一个更奇怪的情况,请参阅我的回答。 - CrazyPyro
我也遇到了这个问题。从我的本地部署可以工作,但从构建服务器上却不行。原来是构建服务器没有在包中包含TypeScript编译器生成的.js文件。可能是构建服务器上的TypeScript工具版本较旧。为了快速解决,我将.js文件包含在项目中。 - stimms
对我来说,问题出在使用 BitTorrent Sync 部署文件时。由于某些故障,一些文件根本没有被部署。 - Filip

10
这是我写的一个快速类,可以使这个过程更容易。
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);
    }
}

要使用它,只需在代码中将ScriptBundle替换为BundleRelaxed,如下所示:

        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")
            );

2
很好的例子 - 唯一需要注意的是,HostingEnvironment.MapPath 不考虑您可能正在使用的 BundleTable.VirtualPathProvider 扩展(可能是非默认的,而不是 HostingEnvironment.VirtualPathProvider)。对于这种情况,您需要将上面的示例转换为使用 BundleTable.VirtualPathProvider.DirectoryExistsBundleTable.VirtualPathProvider.GetDirectory文件模式搜索变得有些棘手,但这是一个好的起点。 - SliverNinja - MSFT
这对我解决了问题。仍然没弄清是哪个捆绑包有问题。感谢你提供这个强大的代码示例,你让我在今天下午免受进一步的恼火之苦。 - Don Rolling

3

今天我遇到了同样的问题,实际上我发现~/Scripts目录下的一些文件没有发布。发布缺失的文件后,问题得到解决。


3
这也可能是由于部署时的竞争条件引起的:
如果您使用Visual Studio的“发布”功能通过网络文件共享进行部署,并勾选“在发布之前删除所有现有文件”(有时我这样做是为了确保我们不会无意中仍然依赖已从项目中删除但仍然停留在服务器上的文件),那么如果某人在重新部署所有必需的JS/CSS文件之前访问该站点,它将启动Application_Start和RegisterBundles,这将无法正确构建bundles并引发此异常。
但是,当您收到此异常并检查服务器时,所有必需的文件都在应该的位置!
但是,应用程序仍然愉快地继续提供站点,为任何bundle请求生成404,以及由此产生的未经样式化/未经功能化的页面,并且即使现在必需的JS/CSS文件可用,它也永远不会尝试重新构建bundles。
使用“使用本地副本替换匹配文件”进行重新部署将触发应用程序重新启动并正确注册bundles。

2
像@JerSchneid一样,我的问题是空目录,但我的部署过程与OP不同。我在Azure上进行了基于git的部署(使用Kudu),并没有意识到git不包含repo中的空目录。参见https://dev59.com/QnVD5IYBdhLWcg3wBWzd#115992 因此,我的本地文件夹结构如下: [项目根目录]/Content/jquery-plugins //有文件 [项目根目录]/Scripts/jquery-plugins //有文件 [项目根目录]/Scripts/misc-plugins //空文件夹
然而,在远程服务器上克隆/拉取我的存储库时,任何空目录都不会被获取: [项目根目录]/Content/jquery-plugins //有文件 [项目根目录]/Scripts/jquery-plugins //有文件
修复此问题的最佳方法是在空目录中创建.keep文件。请参见该SO解决方案:https://dev59.com/QnVD5IYBdhLWcg3wBWzd#21422128

2

我遇到了同样的问题。我的情况是脚本文件夹中包含的所有bootstrap/jQuery脚本都不在wwwroot文件夹中。一旦我将脚本文件夹添加到wwwroot中,错误就消失了。


2
我也遇到了这个错误,原因是我的bundles.config文件中有不存在的目录。将其更改为:
<?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>

转换为:

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

解决我的问题。

1
我也遇到了同样的问题。浏览到脚本文件夹下的文件路径。复制粘贴文件名,并在bundle.cs中进行更改:
旧代码: //Bundle.cs
public class BundleConfig

{

    public static void RegisterBundles(BundleCollection bundles)

    {

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

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

    }
}

新代码:

public class BundleConfig

{

      public static void RegisterBundles(BundleCollection bundles)

      {

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

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.js"));

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-2.6.2.js"));
      }
}

1

这可能是一个旧问题。我有类似的错误,在我的情况下,Scripts文件夹隐藏在我的Models文件夹中。堆栈跟踪清楚地表示它丢失了目录,并且默认情况下所有Java Scripts应该在Scripts文件夹中。这可能不适用于上述用户。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接