0

其實我得到三個404試圖載入相同的文件,但一個我感興趣的是發生在這裏,在我index.html爲什麼動態加載確實存在的文件時會得到404?

<script> 
    System.import('app/main').catch(function (err) { console.error("IMPORT_ERR: " + err); }); 
</script> 

記錄的錯誤是:

IMPORT_ERR: Error: Error: XHR error (404 Not Found) loading http://localhost:57971/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js 
     at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:57971/js/zone.js:647:29) 
     at ZoneDelegate.invokeTask (http://localhost:57971/js/zone.js:236:37) 
     at Zone.runTask (http://localhost:57971/js/zone.js:136:47) 
     at XMLHttpRequest.ZoneTask.invoke (http://localhost:57971/js/zone.js:304:33) 
    Error loading http://localhost:57971/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js as "@angular/platform-browser-dynamic" from http://localhost:57971/app/main.js 

然而,有問題的文件肯定存在:

PS C:\Dev\Angular2\Projects\Introduction\Code\src> ls TourOfHeroes2\node_modules\@angular\platform-browser-dynamic\bundles 

    Directory: C:\Dev\Angular2\Projects\Introduction\Code\src\TourOfHeroes2\node_modules\@angular\platform-brow 
    ser-dynamic\bundles 

Mode    LastWriteTime   Length Name               
----    -------------   ------ ----               
-a----  2016/09/25 8:50 AM   4631 platform-browser-dynamic-testing.umd.js      
-a----  2016/09/25 8:50 AM   8749 platform-browser-dynamic.umd.js        
-a----  2016/09/25 8:50 AM   4703 platform-browser-dynamic.umd.min.js       

文件夾TourOfHeroes2是Web應用程序的根目錄。

+0

哪裏是索引文件?它位於C:\ Dev \ Angular2 \ Projects \ Introduction \ Code \ src \ TourOfHeroes2 \? –

+0

@AminJafari在TourOfHeroes/wwwroot中,就像ASP.NET Core中的約定一樣。該文件夾以外的任何文件都不能提供。但那又如何呢?在絕對使用的URL,我應該得到一個禁止或什麼,而不是404。對不起,我已經添加了'核心'標籤。 – ProfK

+0

您確定您的節點包位於wwwroot/node_modules中,還是他們位於wwwroot/lib中?常見的約定是有一個lib文件夾。或者你是否爲這些圖書館添加了映射?聽起來像你沒有在System.config的地圖和/或路徑部分中正確的條目。你能把它和你的文件夾結構一起發佈嗎? – Tseng

回答

0

事實證明,該應用程序不允許請求node_modules文件夾中的靜態文件[1]。我做了很多小的改動應用程序之前,它的工作,我將在這裏今天晚些時候修改,但最重要的是,在Startup

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    app.UseDefaultFiles(); 
    app.UseStaticFiles(); 
    UseCustomFileServer(app, env); 
} 

private void UseCustomFileServer(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    // this will serve up wwwroot 
    app.UseFileServer(); 

    // this will serve up node_modules 
    var provider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "node_modules")); 
    var options = new FileServerOptions(); 
    options.RequestPath = "/node_modules"; 
    options.StaticFileOptions.FileProvider = provider; 
    options.EnableDirectoryBrowsing = true; 
    app.UseFileServer(options); 
} 

由於一些,我想,糟糕的設計,UseStaticFiles只能從一個目錄提供靜態文件,我們需要它爲wwwroot,所以我必須爲node_modules文件源添加UseFileServer

[1] .NET Core Web應用程序不會默認允許任何靜態文件被提供。你必須在Microsoft.AspNetCore.StaticFiles包添加到project.json,而這兩個行添加到Startup.Configure(),以正確的順序:

app.UseDefaultFiles(); 
app.UseStaticFiles(); 
相關問題