asp.net-core - .NetCore:如何在生产中包含文件夹?
问题描述
所以我有这个代码来发送电子邮件。代码尝试.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>
它不工作。
编辑
我试图设置这个:
但仍然无法正常工作。
解决方案
所以我的问题是该Email
文件夹没有复制到已发布的包中。将此代码添加到.csproj
:
<ItemGroup>
<None Include="Email\EmailConfirmation.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Email\ResetPassword.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
或者您可以像这样在项目资源管理器中设置它:
由于某种原因,必须将构建操作设置为None
,因为.NetCore
会区别对待Content
并且不会将文件夹复制到发布包中(可能内容会合并到.dll
中,我不知道)。
推荐阅读
- python - 获得最大置信度的边界框 pandas opencv python
- php - Cakephp 3 Sum 2列同一张表
- android - Android Automotive OS 不显示可播放的 MediaItem
- gradle - Gradle如何获取特定工件的依赖关系树
- javascript - ExcelJS 和格式化文本
- python - 警告:使用 pact-python 验证协议时忽略不支持的匹配规则
- android - 在反序列化 Facebook 关键帧动画的输入流时尝试获取空数组的长度
- python - 如何通过函数退出整个程序?
- python - F1 分数错误:AttributeError:“float”对象没有属性“item”
- sockets - 如何发出套接字 io 变量而不是 "\"text\"" 文本符号