简体   繁体   English

ASP.NET Core在项目目录之外提供文件

[英]ASP.NET Core serving a file outside of the project directory

In my ASP.NET Core project I am trying to serve an html file like this: 在我的ASP.NET Core项目中,我尝试提供这样的html文件:

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

This results in an Error: 这将导致错误:

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

Pasting the path from the ErrorMessage into my browser I am able to open the file, so the file is clearly there. 将ErrorMessage的路径粘贴到我的浏览器中,我可以打开该文件,因此该文件显然位于此处。

The only way I have been able to serve the file is placing it in wwwroot in the project folder and serving it like this: 我能够提供文件的唯一方法是将其放置在项目文件夹的wwwroot中,并按以下方式提供文件:

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

I have already changed the folder I serve static files from using app.UseStaticFiles(options) (which works), so I figured the Controller would use that folder as default, but it keeps looking in wwwroot. 我已经使用app.UseStaticFiles(options)更改了我为静态文件提供服务的文件夹(可以使用),因此我认为Controller会将该文件夹用作默认文件夹,但它会一直在wwwroot中查找。

How can I serve a file that is placed outside of wwwroot, or even outside the project, from a Controller? 如何从Controller为放在wwwroot甚至项目之外的文件提供服务?

You need to use PhysicalFileProvider class, that is an implementation of IFileProvider and is used to access the actual system's files. 您需要使用PhysicalFileProvider类,该类是IFileProvider的实现,用于访问实际系统的文件。 From File providers section in documentation: 文档的“文件提供者”部分:

The PhysicalFileProvider provides access to the physical file system. PhysicalFileProvider提供对物理文件系统的访问。 It wraps the System.IO.File type (for the physical provider), scoping all paths to a directory and its children. 它包装System.IO.File类型(用于物理提供程序),对目录及其子目录的所有路径进行范围界定。 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. 在ASP.NET Core应用中,您可以直接实例化PhysicalFileProvider提供程序,也可以通过依赖项注入在Controller或服务的构造函数中请求IFileProvider。

Example of how to create a PhysicalFileProvider instance and use it: 如何创建PhysicalFileProvider实例并使用它的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在asp.net Core 2项目中为路线提供角度服务 - Serving angular with routes in asp.net core 2 project 将 SQLite 数据库文件从 class 库添加到 ASP.NET 核心 MVC Z78E6221F6393D13566681DB 项目 build398814CE 目录 - Add SQLite dtabase file from a class library to ASP.NET Core MVC output directory on project build 在asp.net核心项目中获取Azure Active Directory组 - Getting Azure Active Directory groups in asp.net core project ASP.NET Core 的 asp-append-version 属性不适用于 wwwroot 目录之外的静态文件 - ASP.NET Core's asp-append-version attribute not working for static files outside of the wwwroot directory Static 文件未通过 IIS 提供,但通过本地主机提供 - ASP.NET 核心 3.1 MVC 和 ZD7EFAZDA19FBE7D3972FDB602422 - Static file not served through IIS but serving through localhost - ASP.NET Core 3.1 MVC & C# 将 web.config 文件添加到 Asp.Net Core 项目 - Add web.config file to Asp.Net Core project 我的 asp.net 核心项目中没有 .csproj 文件 - NO .csproj file in my asp.net core project 无法从项目目录(asp.net)下载文件 - Can not download file from project directory (asp.net) ASP.NET Core 2.0-提供没有扩展名的文件 - ASP.NET Core 2.0 - Serving files with no extension ASP.NET Core 不提供 robots.txt - ASP.NET Core not serving robots.txt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM