2017-04-13 1 views
2

프로젝트 디렉토리 외부 파일 제공 :ASP.NET 코어 I이 같은 HTML 파일 제공하기 위해 노력하고 내 ASP.NET 핵심 프로젝트에서

FileNotFoundException: Could not find file: c:/path/to/index.html 
: 이것은 오류가 발생

public IActionResult Index() 
{ 
    return File("c:/path/to/index.html", "text/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.

예 : 문서의 File providers 섹션에서/location-of-javascript-files-in-asp-net-core-areas/43257504 # 43257504 –

답변

4

PhysicalFileProvider 클래스를 사용해야하며 이는 구현시 IFileProvider이며 실제 시스템의 파일에 액세스하는 데 사용됩니다.

IFileProvider provider = new PhysicalFileProvider(applicationRoot); 
IDirectoryContents contents = provider.GetDirectoryContents(""); // the applicationRoot contents 
IFileInfo fileInfo = provider.GetFileInfo("wwwroot/js/site.js"); // a file under applicationRoot 
관련 문제