2012-07-03 254 views
98

我剛剛出版了我的項目,我的主機Arvixe和得到這個錯誤(做工精細本地):目錄不存在。參數名:directoryVirtualPath

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 

是什麼意思?

回答

199

我有同樣的問題,並發現我有一些捆綁是指非內已有使用文件{版本}和*通配符如

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

我刪除了所有那些和錯誤走了。

+46

+10,如果我能。令人驚訝的模糊問題。 –

+4

Upvote評論@EricJ。給+10這個答案... – awe

+0

+10000 - 謝謝你,我也和Arvixe一起,永遠不會找到這個。我正在使用一個沒有前端的精簡的Web API項目,因此我根本不需要任何budle.config – Kwakker35

0

我得到了同樣的問題! 它似乎與IIS Express。我更改了IIS Express的項目URL:

"http://localhost:3555/" 

然後問題消失了。

7

這是我寫的一個快速課程,以使它更容易。

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

要使用它,只是BundleRelaxed取代ScriptBundle在你的代碼,如:

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

很好的例子 - 這裏唯一的疑問是'HostingEnvironment.MapPath'沒有考慮到['BundleTable.VirtualPathProvider'](https://aspnetoptimization.codeplex.com/SourceControl/latest#src/System.Web.Optimization/BundleTable .cs)擴展您可能正在使用(_可以是非默認的,而不是'HostingEnvironment.VirtualPathProvider'_)。對於這種情況,您需要將上面的示例轉換爲使用'BundleTable.VirtualPathProvider.DirectoryExists'和'BundleTable.VirtualPathProvider.GetDirectory'。 **文件模式**搜索變得更有問題,但開始的好地方。 – SliverNinja

+0

這解決了我的問題。還沒有弄清楚誰是這個罪魁禍首。謝謝你這個功能強大的代碼示例,你今天下午讓我免於進一步惡化。 –

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> 

解決這個問題對我來說。

15

我有這個相同的問題,它不是一個代碼問題。 我使用的是發佈選項(而不是FTP版本),Visual Studio沒有將我的一些腳本/ css上傳到Azure服務器,因爲它們沒有被「包含在我的項目中」。 因此,本地它工作得很好,因爲文件在我的硬盤上。 在我的情況下,解決這個問題是「項目>顯示所有文件...」,並右鍵單擊那些未包括的,包括它們並再次發佈

+0

+1這是一個比接受的更好的答案,也許應該合併到它。由於對「未找到文件/目錄」做出響應的第一個邏輯事項就是檢查它是否存在。但在這種情況下,它有點偷偷摸摸,因爲你檢查並確實存在於本地,而不是在服務器上。對於一個更奇怪的情況,請參閱我的答案。 – CrazyPyro

+0

我也有這個問題。從我的本地盒子部署工作,但從構建服務器沒有。原來,構建服務器不包括由包中的TypeScript編譯器生成的.js文件。可能是構建服務器上的舊版本的TypeScript工具。作爲一個快速解決方案,我在項目中包含了.js文件。 – stimms

+0

對我來說,這是用於部署文件的BitTorrent Sync的一個問題。有些文件根本沒有部署,因爲一些小故障.. – Filip

0

這也可能是由於部署時的競爭條件:

如果您使用Visual Studio的「發佈」來部署網絡文件共享,並選中「在發佈之前刪除所有現有文件」。 (我有時會這樣做,以確保我們不會在不知不覺中依賴於已從項目中刪除但仍在服務器上掛起的文件。)

如果有人在所有需要的JS/CSS之前點擊了該網站文件被重新部署,它將啓動Application_StartRegisterBundles,這將無法正確構建捆綁並拋出此異常。

但是當你得到這個異常並去檢查服務器時,所有必要的文件都是正確的地方!

但是,應用程序繼續爲該站點提供服務,爲任何bundle請求生成404以及由此產生的無格式/不可操作頁面,即使在必要的JS/CSS文件之後也不會嘗試重建捆綁包現在可用。

使用「用本地副本替換匹配文件」重新部署將觸發應用程序重新啓動並在此次正確註冊捆綁包。

3

我今天碰到同樣的問題,實際上我發現〜/ Scripts下的一些文件沒有發佈。我發佈丟失的文件後,問題得到解決

0

我的問題是我的網站沒有要綁定的文件。不過,我用一個包含jQuery腳本的MVC模板創建了該網站。 bundle.config引用了這些文件及其文件夾。不需要腳本,我刪除了它們。編輯bundle.config後,一切都很好。

2

就像@JerSchneid,我的問題是空目錄,但我的部署過程與OP不同。 我在Azure上做了一個基於git的部署(使用Kudu),並沒有意識到git不包括回購庫中的空目錄。見https://stackoverflow.com/a/115992/1876622

所以,我的本地文件夾結構爲:

[項目根] /內容/ jQuery的插件//有文件

[項目根] /腳本/ jQuery的插件//民政事務文件

[項目根] /腳本/ MISC-插件//空文件夾

而在REMOT我的倉庫的任何克隆/拉E服務器沒有得到上述空目錄:

[項目根] /內容/ jQuery的插件//有文件

[項目根] /腳本/ jQuery的插件//有文件

解決此問題的最佳方法是在空目錄中創建一個.keep文件。看到這樣的解決方案:https://stackoverflow.com/a/21422128/1876622

1

這可能是一個老問題。我有類似的錯誤,在我的情況下,它是隱藏在我的模型文件夾中的腳本文件夾。堆棧跟蹤清楚地說明了其缺少的目錄,並且默認情況下所有的Java腳本都應該位於腳本文件夾中。這可能不適用於上述用戶。

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

,但我已經沒有創建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/

0

所有工作正常,然後同時使不相關的變化和未來建設跨越了同樣的問題來了。使用源代碼控制與以前的版本進行比較,發現我的../Content/Scripts文件夾被神祕地清空了!

恢復../Content/Scripts/*.*從一個備份和所有運作良好!

ps:使用VS2012,MVC4,最近更新了一些NuGet包,所以可能在這個問題上起了一定的作用,但所有更新後都運行良好,所以不確定。

1

我也面臨同樣的問題。在bundle.cs瀏覽到下的腳本文件路徑Folder.Copied確切的文件名和取得的變化:

舊代碼: //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")); 
     } 
} 
0

凝視你的BundleConfig.cs文件進行調用IncludeDirectory()

即行:

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

我的網格目錄不存在。

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

我不得不刷新應用程序池我共享主機的控制面板來解決這個問題上。

1

我在VS2015中打開一個VS2017項目時,遇到了這個問題,構建瞭解決方案,然後上載了這些DLL。

在VS2017中重建它並重新上傳DLL修復了問題。

1

我有同樣的問題。在我的情況下,問題是包含所有bootstrap/jqueries腳本的腳本文件夾不在wwwroot文件夾中。一旦我將腳本的文件夾添加到wwwroot,錯誤消失了。

0

從bundleConfig.cs類文件中刪除這個代碼行解決了我的難題:

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