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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11317384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:22:54  来源:igfitidea点击:

Directory does not exist. Parameter name: directoryVirtualPath

c#asp.net-mvciisasp.net-mvc-4

提问by BjarkeCK

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

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

What does it mean ?

这是什么意思 ?

采纳答案by Martin ?rding-Thomsen

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

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

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

I removed all of those and the error went away.

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

回答by user2320546

I got the same question! It seems to with IIS Express. I change the IIS Express' URL for Project Like:

我有同样的问题!它似乎与 IIS Express。我将 IIS Express 的 URL 更改为 Project Like:

"http://localhost:3555/"

then the problem gone.

那么问题就解决了。

回答by Barnabas Kendall

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:

要使用它,只需在代码中将 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")
            );

回答by JerSchneid

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

我的 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>

To this:

对此:

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

Solve the problem for me.

为我解决问题。

回答by dsnunez

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

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

回答by CrazyPyro

This can also be caused by a race condition while deploying:

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

If you use Visual Studio's "Publish" to deploy over a network file share, and check "Delete all existing files prior to publish." (I do this sometimes to ensure that we're not unknowingly still depending on files that have been removed from the project but are still hanging out on the server.)

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

If someone hits the site before all the required JS/CSS files are re-deployed, it will start Application_Startand RegisterBundleswhich will fail to properly construct the bundles and throw this exception.

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

But by the time you get this exception and go check the server, all the necessary files are right where they should be!

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

However, the application happily continues to serve the site, generating 404's for any bundle request, along with the unstyled/unfunctional pages that result from this, and never tries to rebuild the bundles even after the necessary JS/CSS files are now available.

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

A re-deploy using "Replace matching files with local copies" will trigger the app to restart and properly register the bundles this time.

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

回答by Naga

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

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

回答by CraigP

My problem was that my site had no files to bundle. However, I had created the site with an MVC template, which includes jQuery scripts. The bundle.config referred to those files and their folders. Not needing the scripts, I deleted them. After editing the bundle.config, all was good.

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

回答by HeyZiko

Like @JerSchneid, my problem was empty directories, but my deployment process was different from the OP. I was doing a git-based deploy on Azure (which uses Kudu), and didn't realize that git doesn't include empty directories in the repo. See https://stackoverflow.com/a/115992/1876622

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

So my local folder structure was:

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

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

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

[Project Root]/Scripts/misc-plugins // empty folder

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

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

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

Whereas any clone / pull of my repository on the remote server was not getting said empty directory:

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

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

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

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

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

The best approach to fixing this is to create a .keep file in the empty directory. See this SO solution: https://stackoverflow.com/a/21422128/1876622

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

回答by CuriousRK

This may be an old issue.I have similar error and in my case it was Scripts folder hiding in my Models Folder. Stack trace clearly says its missing Directory and by default all Java Scripts should be in Scripts Folder. This may not be applicable to above users.

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