首页 > 解决方案 > .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>

它不工作。


编辑

我试图设置这个:

在此处输入图像描述

但仍然无法正常工作。

标签: asp.net-coreasp.net-core-mvcasp.net-core-webapi

解决方案


所以我的问题是该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中,我不知道)。


推荐阅读