2017-04-13 133 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 
相关问题