【发布时间】:2022-11-10 11:37:19
【问题描述】:
我在 Azure 应用服务上运行了一个 asp.net core 5 应用程序,它应该获取一个用作电子邮件模板的 .cshtml 文件。我使用IWebHostEnvironment 接口的.ContentRootPath 属性返回路径的第一部分,并将路径的其余部分与文件连接起来,并带有一个字符串。在本地,一切正常,但是当应用程序在 Azure 中运行时获取 .cshtml 文件有问题。
获取 .cshtml 时,观察到以下错误:
找不到路径“/home/site/wwwroot/wwwroot/Templates/Email/EmailRegistroAssinatura.cshtml”的一部分
但在本地,这一切都没有发生。我希望 .ContentRootPath 属性根据应用程序运行的环境返回路径的第一部分。
我的代码如下:
using Microsoft.AspNetCore.Hosting; string projectRootPath = _hostingEnvironment.ContentRootPath; string file = "wwwroot/Templates/Email/" + fileName + ".cshtml"; string path = Path.Combine(projectRootPath, file); using (StreamReader reader = File.OpenText(path)) { htmlStringEmail = reader.ReadToEnd(); }在两种环境中使用相同的代码如何正确获取路径?
【问题讨论】:
-
你试过
_hostEnvironment.WebRootPath吗? -
是的,但它也没有工作。
-
您需要绝对路径,获取
System.IO.Directory.GetCurrentDirectory(),然后附加相对文件路径并尝试 -
@viveknuna,我不断收到与 GetCurrentDirectory() 相同的错误
标签: c# azure asp.net-core