1、注入IWebHostEnvironment
private IWebHostEnvironment _hostEnvironment; public FormBaseController(IWebHostEnvironment hostEnvironment) { _hostEnvironment = hostEnvironment; }
2、读取根目录下文件内容(Templates/Index.html)
public async Task<IActionResult> GetFormPageFileName() { IFileProvider fileProvider = this._hostEnvironment.ContentRootFileProvider; IFileInfo fileInfo = fileProvider.GetFileInfo("Templates/Index.html"); string fileContent = null; using (StreamReader readSteam = new StreamReader(fileInfo.CreateReadStream())) { fileContent = await readSteam.ReadToEndAsync(); } return View(fileContent); }
标签:Templates,文件,NetCore,Index,获取,IWebHostEnvironment,fileContent,hostEnvironment,fi From: https://www.cnblogs.com/microsoft-zh/p/17984846