7

I have a html file that is an email template. In my Azure function I want to read the file in with 'HtmlDocument', manipulate it, then send it out as an email to members.

How do I read the file 'hero.html' from my Azure Function app? And then once I publish it to Azure will I need to change the way the file is read?

FYI - doc.Load accepts a string, file path and other parameters

Here is what I tried and a pic of my project.

        //var emailBodyText = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Templates", "hero.html"));
        var mappedPath = Path.GetFullPath("hero.html");

        var doc = new HtmlDocument();
        doc.Load(mappedPath);

enter image description here

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • Possible duplicate of [Including a file when I publish my azure function in Visual Studio](https://stackoverflow.com/questions/46537758/including-a-file-when-i-publish-my-azure-function-in-visual-studio) – Bruce Chen Oct 04 '17 at 01:26

2 Answers2

11

You can get the function directory name from ExecutionContext parameter, if you add it to the function:

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
    var path = $"{context.FunctionDirectory}\\Templates\\hero.html";
    // ...
}

See details in Retrieving information about the currently running function.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
0

Can access path directly in Azure function code (@"C:\home\site\wwwroot\wwwroot\Templates\EmailTemplate\CommonStatusTemplate.html")

sachind
  • 387
  • 5
  • 8