120

我刚刚在 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

这是什么意思 ?

4

20 回答 20

237

我遇到了同样的问题,发现我有一些包使用 {version} 和 * 通配符指向不存在的文件,例如

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

我删除了所有这些,错误就消失了。

于 2012-10-04T16:25:24.810 回答
15

我有同样的问题,这不是代码问题。我使用的是发布选项(不是 FTP 选项),Visual Studio 没有将我的一些脚本/css 上传到 azure 服务器,因为它们没有“包含在我的项目中”。所以,在本地它工作得很好,因为文件在我的硬盘里。在我的情况下解决此问题的是“项目>显示所有文件...”并右键单击未包含的文件,将它们包含并再次发布

于 2014-01-27T05:25:09.467 回答
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")
            );
于 2013-06-17T21:11:04.107 回答
3

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

于 2014-04-20T16:44:50.333 回答
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>

为我解决问题。

于 2014-01-17T09:11:35.033 回答
2

这也可能是由部署时的竞争条件引起的:

如果您使用 Visual Studio 的“发布”通过网络文件共享进行部署,请选中“发布前删除所有现有文件”。(我有时这样做是为了确保我们不会在不知不觉中仍然依赖于已从项目中删除但仍在服务器上的文件。)

如果有人在重新部署所有必需的 JS/CSS 文件之前访问了该站点,它将启动Application_Start并且RegisterBundles无法正确构建包并抛出此异常。

但是当你得到这个异常并去检查服务器时,所有必要的文件都在它们应该在的地方!

但是,应用程序很高兴地继续为站点提供服务,为任何捆绑请求生成 404,以及由此产生的无样式/无功能页面,并且即使在必要的 JS/CSS 文件现在可用之后,也不会尝试重新构建捆绑包。

使用“用本地副本替换匹配的文件”重新部署将触发应用程序重新启动并这次正确注册捆绑包。

于 2014-02-14T19:47:56.073 回答
2

像@JerSchneid 一样,我的问题是空目录,但我的部署过程与 OP 不同。我在 Azure(使用 Kudu)上进行基于 git 的部署,但没有意识到 git 在 repo 中不包含空目录。见https://stackoverflow.com/a/115992/1876622

所以我的本地文件夹结构是:

[项目根目录]/Content/jquery-plugins // 有文件

[项目根目录]/Scripts/jquery-plugins // 有文件

[项目根目录]/Scripts/misc-plugins // 空文件夹

而远程服务器上我的存储库的任何克隆/拉取都没有得到空目录:

[项目根目录]/Content/jquery-plugins // 有文件

[项目根目录]/Scripts/jquery-plugins // 有文件

解决此问题的最佳方法是在空目录中创建一个 .keep 文件。请参阅此 SO 解决方案: https ://stackoverflow.com/a/21422128/1876622

于 2014-09-23T12:30:08.473 回答
2

我遇到过同样的问题。就我而言,问题在于包含所有 bootstrap/jqueries 脚本的脚本文件夹不在 wwwroot 文件夹中。一旦我将脚本的文件夹添加到 wwwroot,错误就消失了。

于 2017-10-05T19:32:56.360 回答
1

这可能是一个老问题。我有类似的错误,在我的情况下,它是隐藏在我的模型文件夹中的脚本文件夹。堆栈跟踪清楚地说明了它缺少的目录,默认情况下所有 Java 脚本都应该在 Scripts 文件夹中。这可能不适用于以上用户。

于 2016-01-07T04:11:30.640 回答
1

我创建了一个新Angular应用程序并编写了

bundles.Add(new ScriptBundle("~/bundles/app")
    .IncludeDirectory("~/Angular", "*.js")
    .IncludeDirectory("~/Angular/directives/shared", "*.js")
    .IncludeDirectory("~/Angular/directives/main", "*.js")
    .IncludeDirectory("~/Angular/services", "*.js"));

但我创建了 no services,因此该services文件夹未在发布时部署,因为它是空的。不幸的是,您必须在任何空文件夹中放置一个虚拟文件才能发布

https://blogs.msdn.microsoft.com/webdevelopertips/2010/04/29/tip-105-did-you-know-how-to-include-empty-directory-when-package-a-web-application/

于 2016-03-24T20:59:00.837 回答
1

我也面临同样的问题。浏览到 Script Folder 下的文件路径。复制了确切的文件名并在 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"));
      }
}
于 2016-09-04T05:55:42.377 回答
1

当我在 VS2015 中打开一个 VS2017 项目,构建解决方案然后上传 DLL 时,我遇到了这个问题。

在 VS2017 中重建它并重新上传 DLL 解决了这个问题。

于 2017-08-11T14:46:46.797 回答
0

我也有同样的问题!似乎与 IIS Express 有关。我更改了项目的 IIS Express 的 URL,例如:

"http://localhost:3555/"

然后问题就消失了。

于 2013-04-25T15:55:16.120 回答
0

我的问题是我的网站没有要捆绑的文件。但是,我使用包含 jQuery 脚本的 MVC 模板创建了该站点。bundle.config 引用了这些文件及其文件夹。不需要脚本,我删除了它们。编辑 bundle.config 后,一切都很好。

于 2014-07-29T20:52:57.763 回答
0

一切正常,然后在进行不相关的更改时,在下一次构建时遇到了同样的问题。使用源代码管理与以前的版本进行比较,发现我的 ../Content/Scripts 文件夹被神秘地清空了!

从备份中恢复 ../Content/Scripts/*.* 一切正常!

ps:使用VS2012,MVC4,最近更新了一些NuGet包,所以这可能是问题的一部分,但更新后一段时间都运行良好,所以不确定。

于 2016-03-27T18:26:05.853 回答
0

查看 BundleConfig.cs 文件中调用IncludeDirectory()的行

IE:

  bundles.Add(new Bundle("~/bundle_js_angularGrid").IncludeDirectory(
                       "~/Scripts/Grid", "*.js", true));

我的 Grid 目录不存在。

于 2017-05-07T01:52:54.427 回答
0

当我将所有分离的捆绑包合并为一个捆绑包时,我也遇到了这个错误。

bundles.Add(new ScriptBundle("~/bundles/one").Include(
            "~/Scripts/one.js"));
bundles.Add(new ScriptBundle("~/bundles/two").Include(
            "~/Scripts/two.js"));

变成

bundles.Add(new ScriptBundle("~/bundles/js").Include(
            "~/Scripts/one.js",
            "~/Scripts/two.js"));

我必须刷新共享主机控制面板上的应用程序池才能解决此问题。

于 2017-07-08T21:36:56.550 回答
0

从 bundleConfig.cs 类文件中删除这行代码解决了我的挑战:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
于 2018-01-18T08:27:44.700 回答
0

这些答案都没有帮助我,因为我jsx以一种奇怪的方式创建了我的文件。我的代码在本地主机模式下工作,但在生产中失败。

我的解决方法是进入csproj文件并将文件路径从更改<None ...<Content ...

于 2018-04-19T21:36:17.773 回答
0

基本上堆栈跟踪为您提供了删除不存在资源所需的确切位置(如屏幕截图中突出显示的那样)。

显示堆栈跟踪的图像

于 2019-08-18T16:48:32.950 回答