Alvin Stefanus
Alvin Stefanus

Reputation: 2153

.NetCore: How to include folder in production?

So I have this code to send email. The code tries to find the .cshtml for the email template.

This is my folder hierarchy:

Project
-- Email
---- EmailConfirmation.cshtml
---- ResetPassword.cshtml

This is the code to find the template:

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;
}

In Debug, this runs perfectly, but in production, the code cannot find the template.

How can I include the template in publish package?

I tried this in my .csproj:

<ItemGroup>
    <Folder Include="ClientApp\" />
    <Folder Include="Migrations\" />
    <Views Include="Email\**" />
</ItemGroup>

It is not working.


Edit

I tried to set this:

enter image description here

But still not working.

Upvotes: 0

Views: 1951

Answers (2)

Alvin Stefanus
Alvin Stefanus

Reputation: 2153

So my problem was the Email folder was not copied to the published package. Add this code to .csproj:

  <ItemGroup>
    <None Include="Email\EmailConfirmation.cshtml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Include="Email\ResetPassword.cshtml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

Or you can set it in the project explorer like this:

enter image description here

For some reason, the build action has to be set to None, because .NetCore will treat Content differently and the folder will not be copied to the publishing package (maybe the content will be merge into .dll, I do not know).

Upvotes: 2

Boris EKUE-HETTAH
Boris EKUE-HETTAH

Reputation: 157

Could you try this ?

string workingDirectory = Environment.CurrentDirectory;

string projectDirectory=Directory.GetParent(workingDirectory).Parent.Parent.FullName;

It will give you the Path to the project directory. Then you complete with the subdirectory which leads to your file.

Upvotes: 0

Related Questions