Api 如何从asp.net核心应用的wwwroot文件夹中获取文件的虚拟路径?

Api 如何从asp.net核心应用的wwwroot文件夹中获取文件的虚拟路径?,api,asp.net-core,asp.net-web-api,directory,directory-structure,Api,Asp.net Core,Asp.net Web Api,Directory,Directory Structure,我想获取index.html文件的虚拟路径,该文件位于asp.net核心web API的wwwroot文件夹中。我已附上我的代码图片,这将解释这个问题的情况 现在我只是在使用它的完整路径,这不是像这样使用路径的方便方法,所以我想获得这个文件的虚拟路径,这样在任何其他服务器上我都不会遇到任何问题。在控制器中注入IHostingEnvironment,并使用它的WebRootPath访问您的文件 using Microsoft.AspNetCore.Hosting; //... public cl

我想获取index.html文件的虚拟路径,该文件位于asp.net核心web API的wwwroot文件夹中。我已附上我的代码图片,这将解释这个问题的情况


现在我只是在使用它的完整路径,这不是像这样使用路径的方便方法,所以我想获得这个文件的虚拟路径,这样在任何其他服务器上我都不会遇到任何问题。

在控制器中注入
IHostingEnvironment
,并使用它的
WebRootPath
访问您的文件

using Microsoft.AspNetCore.Hosting;
//...

public class AccountController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public AccountController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    //...

    string htmlFilePath = Path.Combine(_hostingEnvironment.WebRootPath, "EmailTemplate", "index.html");

    //...
}