【发布时间】:2020-06-28 12:38:49
【问题描述】:
试图访问路径上的文件 - wwwroot/templates/file.txt。它在 Windows 上使用 -_hostingEnvironment.ContentRootPath + "\templates\file.txt" 工作,但相同的路径表示文件不存在。 我错过了什么
【问题讨论】:
标签: linux file asp.net-core azure-webapps
试图访问路径上的文件 - wwwroot/templates/file.txt。它在 Windows 上使用 -_hostingEnvironment.ContentRootPath + "\templates\file.txt" 工作,但相同的路径表示文件不存在。 我错过了什么
【问题讨论】:
标签: linux file asp.net-core azure-webapps
尝试访问路径上的文件 - wwwroot/templates/file.txt。
下面的代码sn -p对我有用,你可以参考一下。
var filepath = Path.Combine(_hostingEnvironment.ContentRootPath, "templates", "file.txt");
var mes = "test message";
if (System.IO.File.Exists(filepath))
{
using (StreamReader file = new StreamReader(filepath))
{
mes = file.ReadLine();
}
}
ViewBag.fp = filepath;
ViewBag.mes = mes;
return View();
请确保该文件确实存在于您服务器上的该文件夹下。
测试结果
【讨论】: