【发布时间】:2021-08-10 10:55:43
【问题描述】:
所以我有这个代码来发送电子邮件。代码尝试为电子邮件模板查找.cshtml。
这是我的文件夹层次结构:
Project
-- Email
---- EmailConfirmation.cshtml
---- ResetPassword.cshtml
这是找到模板的代码:
public static string GetEmailTemplate(string templateName)
{
string path = Path.Combine(Config.Env.ContentRootPath, "Email", templateName);
string content = "";
// This text is added only once to the file.
if (File.Exists(path))
{
// Create a file to write to.
content = File.ReadAllText(path);
}
else
{
throw new Exception("The email template could not be found");
}
return content;
}
在Debug 中,运行完美,但在生产中,代码找不到模板。
如何将模板包含在发布包中?
我在我的.csproj 中试过这个:
<ItemGroup>
<Folder Include="ClientApp\" />
<Folder Include="Migrations\" />
<Views Include="Email\**" />
</ItemGroup>
它不工作。
编辑
我试图设置这个:
但还是不行。
【问题讨论】:
-
试试这个 var path = Path.GetFullPath(Path.Combine(Config.Env.ContentRootPath, "Email", templateName));
-
你的方法行不通。搜索
render razor view to string,或使用github.com/Tyrrrz/MiniRazor之类的库 -
如果剃刀模板中的逻辑为零,则可以将它们标记为
EmbeddedResource,并在运行时从程序集中读取它们,但如果需要通过渲染阶段,请参阅我之前的评论
标签: asp.net-core asp.net-core-mvc asp.net-core-webapi