2017-04-13 89 views
2

在我的ASP.NET核心的項目,我想成爲一個HTML文件中像這樣的外部文件:ASP.NET核心服務項目目錄

public IActionResult Index() 
{ 
    return File("c:/path/to/index.html", "text/html"); 
} 

這將導致一個錯誤:

FileNotFoundException: Could not find file: c:/path/to/index.html 

將ErrorMessage的路徑粘貼到瀏覽器中我可以打開文件,因此文件顯然存在。

我已經能夠提供服務的文件的唯一方法是將其放置在wwwroot文件在項目文件夾和服務它是這樣的:

public IActionResult Index() 
{ 
    return File("index.html", "text/html"); 
} 

我已經改變了該文件夾我使用app.UseStaticFiles(options)提供靜態文件(工作),所以我想控制器將使用該文件夾作爲默認,但它繼續尋找wwwroot。

如何從控制器提供放置在wwwroot之外甚至項目之外的文件?

+0

見我的回答對另一個類似的問題在這裏http://stackoverflow.com/questions/43256864:如何創建一個PhysicalFileProvider實例,並使用它

The PhysicalFileProvider provides access to the physical file system. It wraps the System.IO.File type (for the physical provider), scoping all paths to a directory and its children. This scoping limits access to a certain directory and its children, preventing access to the file system outside of this boundary. When instantiating this provider, you must provide it with a directory path, which serves as the base path for all requests made to this provider (and which restricts access outside of this path). In an ASP.NET Core app, you can instantiate a PhysicalFileProvider provider directly, or you can request an IFileProvider in a Controller or service's constructor through dependency injection.

例/位置的,JavaScript的文件,在-ASP淨核心區/ 43257504#43257504 –

回答

4

您需要使用PhysicalFileProvider類,即實現IFileProvider並用於訪問實際系統的文件。從在文檔File providers部分:

IFileProvider provider = new PhysicalFileProvider(applicationRoot); 
IDirectoryContents contents = provider.GetDirectoryContents(""); // the applicationRoot contents 
IFileInfo fileInfo = provider.GetFileInfo("wwwroot/js/site.js"); // a file under applicationRoot 
相關問題