html 模板未加载:找不到路径“/Helpers/Email/Templates/template.html”的一部分
html template not loading: Could not find a part of the path '/Helpers/Email/Templates/template.html'
我正在使用 .Net Core 2.1 Web Api。
我为此解决方案使用了 4 个项目。我正在尝试加载一个 html 模板,在本地工作正常,但是一旦我将它推送到 Linux 服务器,文件就无法加载,因为这个错误:
html template not loading: Could not find a part of the path
'/Helpers/Email/Templates/template.html'
下一个解决方案的结构(包括文件夹和 html 文件):
- Core
- Helpers
- Email
EMailHelper.cs
- Templates
template.html
- Infrastructure
- WebApi (this is the main project, with appsettings.json, ...)
好吧,当我尝试从 "EmailHelper.cs" 加载 "template.html" 时,在本地 Windows 和 Mac 都可以正常工作,但是当我将其上传到发布环境 (Azure - Linux),它总是抛出先前的异常。
"template.html" 文件有 "copy at the same directory" 并设置为 "content" 作为构建操作。
还尝试使用这些 url 加载文件:
const string htmlTemplate = "../Helpers/Email/Templates/template.html";
和
string emailTemplateUserRegistration = string.Format("..{0}Helpers{0}Email{0}Templates{0}{template.html}", Path.DirectorySeparatorChar);
但是加载不出来
你知道这是什么原因吗?我该如何解决?
提前致谢。
编辑:
奇怪,如果我加载这个 url 无法从项目加载文件:
/home/project/backend/Helpers/Email/Templates/template.html
但使用 vi 打开文件:
vi /home/project/backend/Helpers/Email/Templates/template.html
加载文件的方法是下一个:
var builder = new StringBuilder();
using (var reader = File.OpenText(htmlTemplate))
{
builder.Append(reader.ReadToEnd());
}
这可能是因为您在 运行 时间在您的 Azure 环境中使用了错误的根路径。尝试使用依赖注入从 HostingEnviroment 获取路径。
public class MyController{
MyController(IHostingEnvironment env){
var path = env.ContentRootPath;
var myTemplatePath = path + "/Helpers/Email/Templates/template.html"
}
}
终于解决了
我不得不说我忘了提到我们在 Azure 中使用 Docker。
所以问题是当您发布应用程序时,所有文件都位于 docker 配置中定义的文件夹中,即:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
因此在 /app 文件夹中,所有资源文件都被复制到,所以而不是 url:
/home/project/backend/Helpers/Email/Templates/template.html
它有:
/app/Email/Templates/template.html
所以在我的 "htmlTemplate" 变量中更改此 url 工作正常:
const string templateUrl = @"Email/Templates/template.html";
我在部署到 AWS ECS 时使用了以下对我有用的代码:
var buildDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var htmlBase = File.ReadAllText(buildDir + @"/Email/Templates/Base.html");
我确定 Base.html 和任何其他 html 文件是:
- 生成操作:None
- 复制到输出目录:始终复制(如果较新则复制也可能有效)
最后,当我第一次部署时,我使用了反斜杠:
var htmlBase = File.ReadAllText(buildDir + @"\Email\Templates\Base.html");
这在 windows 上工作正常,但在 Linux 环境中失败。
更改为正斜杠后一切正常。
我正在使用 .Net Core 2.1 Web Api。
我为此解决方案使用了 4 个项目。我正在尝试加载一个 html 模板,在本地工作正常,但是一旦我将它推送到 Linux 服务器,文件就无法加载,因为这个错误:
html template not loading: Could not find a part of the path '/Helpers/Email/Templates/template.html'
下一个解决方案的结构(包括文件夹和 html 文件):
- Core
- Helpers
- Email
EMailHelper.cs
- Templates
template.html
- Infrastructure
- WebApi (this is the main project, with appsettings.json, ...)
好吧,当我尝试从 "EmailHelper.cs" 加载 "template.html" 时,在本地 Windows 和 Mac 都可以正常工作,但是当我将其上传到发布环境 (Azure - Linux),它总是抛出先前的异常。
"template.html" 文件有 "copy at the same directory" 并设置为 "content" 作为构建操作。
还尝试使用这些 url 加载文件:
const string htmlTemplate = "../Helpers/Email/Templates/template.html";
和
string emailTemplateUserRegistration = string.Format("..{0}Helpers{0}Email{0}Templates{0}{template.html}", Path.DirectorySeparatorChar);
但是加载不出来
你知道这是什么原因吗?我该如何解决?
提前致谢。
编辑:
奇怪,如果我加载这个 url 无法从项目加载文件:
/home/project/backend/Helpers/Email/Templates/template.html
但使用 vi 打开文件:
vi /home/project/backend/Helpers/Email/Templates/template.html
加载文件的方法是下一个:
var builder = new StringBuilder();
using (var reader = File.OpenText(htmlTemplate))
{
builder.Append(reader.ReadToEnd());
}
这可能是因为您在 运行 时间在您的 Azure 环境中使用了错误的根路径。尝试使用依赖注入从 HostingEnviroment 获取路径。
public class MyController{
MyController(IHostingEnvironment env){
var path = env.ContentRootPath;
var myTemplatePath = path + "/Helpers/Email/Templates/template.html"
}
}
终于解决了
我不得不说我忘了提到我们在 Azure 中使用 Docker。
所以问题是当您发布应用程序时,所有文件都位于 docker 配置中定义的文件夹中,即:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
因此在 /app 文件夹中,所有资源文件都被复制到,所以而不是 url:
/home/project/backend/Helpers/Email/Templates/template.html
它有:
/app/Email/Templates/template.html
所以在我的 "htmlTemplate" 变量中更改此 url 工作正常:
const string templateUrl = @"Email/Templates/template.html";
我在部署到 AWS ECS 时使用了以下对我有用的代码:
var buildDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var htmlBase = File.ReadAllText(buildDir + @"/Email/Templates/Base.html");
我确定 Base.html 和任何其他 html 文件是:
- 生成操作:None
- 复制到输出目录:始终复制(如果较新则复制也可能有效)
最后,当我第一次部署时,我使用了反斜杠:
var htmlBase = File.ReadAllText(buildDir + @"\Email\Templates\Base.html");
这在 windows 上工作正常,但在 Linux 环境中失败。
更改为正斜杠后一切正常。