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

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

这是什么意思?

87905 次浏览

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

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

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

我也有同样的问题! 我修改了 IIS Express 的项目 URL,比如:

"http://localhost:3555/"

then the problem gone.

这里有一个简短的类,我写的,使这更容易。

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

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>

帮我解决这个问题。

I had this same issue and it was not a code problem. 我使用的是发布选项(而不是 FTP 选项) ,而且 Visual Studio 没有将我的一些脚本/css 上传到蔚蓝色的服务器上,因为它们没有“包含在我的项目中”。 因此,在本地它工作得很好,因为文件在我的硬盘驱动器。 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

这也可能是由于部署时的竞态条件造成的:

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

如果有人在重新部署所有需要的 JS/CSS 文件之前访问站点,它将启动 Application_StartRegisterBundles,这将无法正确构造捆绑包并抛出此异常。

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

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

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

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

我的问题是,我的网站没有要捆绑的文件。但是,我使用 MVC 模板创建了这个站点,其中包括 jQuery 脚本。Config 引用这些文件及其文件夹。不需要剧本,我把它们删了。在编辑 bundle.config 之后,一切正常。

Like @JerSchneid, my problem was empty directories, but my deployment process was different from the OP. 我在 Azure 上做了一个基于 git 的部署(使用 Kudu) ,没有意识到 git 在回购中不包含空目录。参见 https://stackoverflow.com/a/115992/1876622

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

[ Project Root ]/Content/jquery-plugins//had files

[ Project Root ]/Scripts/jquery-plugins//had files

[ Project Root ]/Scripts/misc-plugins//void 文件夹

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

[ Project Root ]/Content/jquery-plugins//had files

[ Project Root ]/Scripts/jquery-plugins//had files

解决这个问题的最佳方法是创建一个。将文件保存在空目录中。看这个 SO 解决方案: https://stackoverflow.com/a/21422128/1876622

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

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

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

但是我没有创建 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/

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

Restored ../Content/Scripts/*.*from a backup and all worked well!

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

我也面临着同样的问题。浏览到“脚本文件夹”下的文件路径。复制了确切的文件名并在 bundle.cs 中进行了更改:

Old Code : //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"));
}
}

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

即:

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

我的网格目录不存在。

I also had this error when I combined all my separated bundles into one bundle.

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

我必须在我的共享主机的控制面板 刷新应用程序池来解决这个问题。

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

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

I had the same issue. the problem in my case was that the script's folder with all the bootstrap/jqueries scripts was not in the wwwroot folder. once I added the script's folder to wwwroot the error went away.

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

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

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

对我来说,修复是进入 csproj文件并将文件路径从 <None ...更改为 <Content ...

Basically stack trace gives you the exact place (as highlighted in screenshot) you need to remove non-existing resource.

image showing stack trace