目录不存在。参数名称:directoryVirtualPath
Posted
技术标签:
【中文标题】目录不存在。参数名称:directoryVirtualPath【英文标题】:Directory does not exist. Parameter name: directoryVirtualPath 【发布时间】:2012-07-03 18:58:33 【问题描述】:我刚刚在 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
什么意思?
【问题讨论】:
请检查您是否已将脚本文件夹上传到服务器。 【参考方案1】:我遇到了同样的问题,发现我有一些使用 version 和 * 通配符指向不存在文件的包,例如
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-version.js"));
我删除了所有这些,错误就消失了。
【讨论】:
不确定这是如何“令人惊讶地晦涩难懂”或很难找到; stacktrace 将您直接指向来自Application_Start
的BundleConfig.RegisterBundles
调用我的+1 转到@user2465004 的答案。
我收到了同样的错误,因为我的包中引用的 /scripts/ 文件夹在我的服务器上不存在。
我将一个asp.net mvc项目转换为web api,真的没有使用jquery、css文件。很高兴我找到了你的帖子。修复它,一切正常。
除此之外,在发布到 Azure 时,它似乎不允许您发布空文件夹。我有一个 .IncludeDirectory("~/Scripts/Create/Controllers", "*.js") 语句,虽然 Controllers 文件夹确实存在,但它实际上还没有任何内容,这导致了同样的错误。我只是在文件夹中放了一个空白文本文件,然后它就起作用了。
这发生在我的捆绑配置中包含空目录时,我计划在将来添加文件。在本地一切都很好,因为这些目录存在,但是当我推送到 Azure 时,它们没有被创建,【参考方案2】:
我遇到了同样的问题,这不是代码问题。 我使用的是发布选项(不是 FTP 选项),Visual Studio 没有将我的一些脚本/css 上传到 azure 服务器,因为它们没有“包含在我的项目中”。 所以,在本地它工作得很好,因为我的硬盘里有文件。 在我的案例中解决此问题的是“项目>显示所有文件...”并右键单击未包含的文件,将它们包含并再次发布
【讨论】:
+1 这是一个比接受的答案更好的答案,可能应该合并到其中。由于响应“找不到文件/目录”的第一件合乎逻辑的事情已经是去检查它是否存在。但在这种情况下,它有点偷偷摸摸,因为你检查它确实存在于本地,只是不在服务器上。对于更奇怪的情况,请参阅我的回答。 我也有这个问题。从我的本地机器部署有效,但从构建服务器部署却没有。原来是构建服务器没有在包中包含由 TypeScript 编译器生成的 .js 文件。可能是构建服务器上的旧版 TypeScript 工具。作为快速修复,我在项目中包含了 .js 文件。 对我来说,用于部署文件的 BitTorrent Sync 存在问题。由于某些故障,一些文件根本没有部署..【参考方案3】:这是我写的一个快速课程,目的是让这更容易。
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")
);
【讨论】:
很好的例子 - 这里唯一的问题是HostingEnvironment.MapPath
没有考虑您可能正在使用的BundleTable.VirtualPathProvider
扩展(可能是非默认的,而不是HostingEnvironment.VirtualPathProvider
)。对于这种情况,您需要将上面的示例转换为使用BundleTable.VirtualPathProvider.DirectoryExists
和BundleTable.VirtualPathProvider.GetDirectory
。 文件模式搜索变得有点问题,但很好的起点。
这为我解决了这个问题。仍然没有弄清楚有问题的捆绑包是谁。感谢您提供这个强大的代码示例,今天下午您让我免于进一步恶化。【参考方案4】:
我今天遇到了同样的问题,实际上是我发现 ~/Scripts 下的一些文件没有发布。在我发布丢失的文件后问题得到解决
【讨论】:
【参考方案5】:我的 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>
帮我解决问题。
【讨论】:
【参考方案6】:这也可能是由部署时的竞争条件引起的:
如果您使用 Visual Studio 的“发布”通过网络文件共享进行部署,请选中“发布前删除所有现有文件”。 (我有时这样做是为了确保我们不会在不知不觉中仍然依赖已从项目中删除但仍在服务器上的文件。)
如果有人在重新部署所有必需的 JS/CSS 文件之前访问该站点,它将启动 Application_Start
和 RegisterBundles
,这将无法正确构建包并抛出此异常。
但是当你得到这个异常并去检查服务器时,所有必要的文件都在它们应该在的地方!
但是,应用程序很高兴地继续为站点提供服务,为任何捆绑请求生成 404,以及由此产生的无样式/无功能页面,并且即使在必要的 JS/CSS 文件现在已经存在之后,也不会尝试重新构建捆绑包可用。
使用“用本地副本替换匹配的文件”重新部署将触发应用重新启动并这次正确注册捆绑包。
【讨论】:
【参考方案7】:像@JerSchneid 一样,我的问题是空目录,但我的部署过程与 OP 不同。 我在 Azure(使用 Kudu)上进行了基于 git 的部署,但没有意识到 git 在 repo 中不包含空目录。见https://***.com/a/115992/1876622
所以我的本地文件夹结构是:
[项目根目录]/Content/jquery-plugins // 有文件
[Project Root]/Scripts/jquery-plugins // 有文件
[项目根目录]/Scripts/misc-plugins // 空文件夹
而远程服务器上我的存储库的任何克隆/拉取都没有得到空目录:
[项目根目录]/Content/jquery-plugins // 有文件
[Project Root]/Scripts/jquery-plugins // 有文件
解决此问题的最佳方法是在空目录中创建一个 .keep 文件。请参阅此 SO 解决方案:https://***.com/a/21422128/1876622
【讨论】:
【参考方案8】:我有同样的问题。就我而言,问题在于包含所有 bootstrap/jqueries 脚本的脚本文件夹不在 wwwroot 文件夹中。一旦我将脚本的文件夹添加到 wwwroot,错误就消失了。
【讨论】:
【参考方案9】:这可能是一个老问题。我有类似的错误,在我的情况下,它是隐藏在我的模型文件夹中的脚本文件夹。堆栈跟踪清楚地说明了它缺少的目录,默认情况下所有 Java 脚本都应该在 Scripts 文件夹中。这可能不适用于上述用户。
【讨论】:
【参考方案10】:我创建了一个新的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/
【讨论】:
【参考方案11】:我也遇到了同样的问题。浏览到 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"));
【讨论】:
【参考方案12】:我在 VS2015 中打开一个 VS2017 项目,构建解决方案然后上传 DLL 时遇到了这个问题。
在 VS2017 中重建它并重新上传 DLL 解决了这个问题。
【讨论】:
【参考方案13】:我也有同样的问题! 似乎与 IIS Express 有关。我更改了 Project Like 的 IIS Express 的 URL:
"http://localhost:3555/"
然后问题就解决了。
【讨论】:
【参考方案14】:我的问题是我的网站没有要捆绑的文件。但是,我使用包含 jQuery 脚本的 MVC 模板创建了该站点。 bundle.config 引用了这些文件及其文件夹。不需要脚本,我删除了它们。编辑 bundle.config 后,一切都很好。
【讨论】:
【参考方案15】:一切正常,然后在进行不相关的更改时,在下一次构建时遇到了同样的问题。使用源代码管理与以前的版本进行比较,发现我的 ../Content/Scripts 文件夹被神秘地清空了!
从备份中恢复了 ../Content/Scripts/*.*,一切正常!
ps:使用 VS2012,MVC4,最近更新了一些 NuGet 包,所以这可能在问题上起了一定的作用,但更新后一段时间都运行良好,所以不确定。
【讨论】:
【参考方案16】:查看您的 BundleConfig.cs 文件中调用 IncludeDirectory()
的行即:
bundles.Add(new Bundle("~/bundle_js_angularGrid").IncludeDirectory(
"~/Scripts/Grid", "*.js", true));
我的 Grid 目录不存在。
【讨论】:
【参考方案17】:当我将所有分开的捆绑包合并为一个捆绑包时,我也遇到了这个错误。
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"));
我必须在共享主机的控制面板上刷新应用程序池才能解决此问题。
【讨论】:
【参考方案18】:从 bundleConfig.cs 类文件中删除这行代码解决了我的挑战:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-version.js"));
【讨论】:
【参考方案19】:这些答案都没有帮助我,因为我以一种奇怪的方式创建了我的 jsx
文件。我的代码在 localhost 模式下工作,但在生产中失败。
我的解决方法是进入csproj
文件并将文件路径从<None ...
更改为<Content ...
【讨论】:
【参考方案20】:基本上,堆栈跟踪会为您提供删除不存在资源所需的确切位置(如屏幕截图中突出显示的那样)。
【讨论】:
以上是关于目录不存在。参数名称:directoryVirtualPath的主要内容,如果未能解决你的问题,请参考以下文章
Shell 脚本检查 dir 目录是不是存在然后更改路径,如果不存在则使用该名称创建 dir 并检查文件名不存在
java.lang.IllegalArgumentException:您尝试使用查询字符串中不存在的字符串名称设置参数值